首页 > 代码库 > WCF header 域

WCF header 域

    [OperationContract]    [WebInvoke(UriTemplate = "poststr1")]    public string poststr1(csinfo cs)    {        var x = new { x = "poststr1的调用", a = "服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  A值:" + cs.a, b = "B值: " + cs.b, c = "C值:" + cs.c };        return Xtools.json_TtoJson(x);    }    [OperationContract]    [WebInvoke(UriTemplate = "poststr2", BodyStyle = WebMessageBodyStyle.WrappedRequest)]    public string poststr2(string cs)    {        var x = new { x = "poststr2的调用", a = "poststr2调用 服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  cs值:" + cs };        return Xtools.json_TtoJson(x);    }

  

function dy3() { var cs = { a: "dsfdsfds", b: "ssddddd", c: "sdfsfds" }; $.ajax({ type: "POST", url: "http://10.10.12.70/xx1100/poststr1", contentType: "application/json; charset=utf-8", //(可以) // data: ‘{"a":"axx","b":"bxx","c:":"cxx"}‘, data: JSON.stringify(cs), dataType: "json", cache: false, async: false, processData: true, success: function (json) { alert(json); var t = eval(‘(‘ + json + ‘)‘); alert(t.x); }, error: function (xhr, ajaxOptions, thrownError) { alert("调用出错" + xhr.responseText); } }); };

  

  <system.webServer>    <httpProtocol>      <customHeaders>        <add name="Access-Control-Allow-Origin" value="http://www.mamicode.com/*"/>        <add name="Access-Control-Allow-Methods" value="http://www.mamicode.com/GET, POST, OPTIONS"/>        <add name="Access-Control-Allow-Credentials" value="http://www.mamicode.com/true"/>        <add name="Access-Control-Allow-Headers" value="http://www.mamicode.com/Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers"/>        <add name="Access-Control-Max-Age" value="http://www.mamicode.com/3600"/>      </customHeaders>    </httpProtocol>  </system.webServer>

  

  public class MessageInspector : IDispatchMessageInspector    {        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)        {                       return request;        }        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)        {            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Origin", "*");            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");            //WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Credentials", "true");            WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Max-Age", "3600");            if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")            {                  }            //if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")            //{            //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");            //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Credentials", "true");            //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");            //    WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Max-Age", "3600");            //    //WebOperationContext.Current.OutgoingResponse.Headers.Set("Access-Control-Allow-Methods", "GET, POST");            //    //HttpContext.Current.Response.End();                            ////WebOperationContext.Current.CreateTextResponse("OK").Close();            //    //WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NoContent;            //}            //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");            //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");            //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");            //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Test-Header, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Headers");            //HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "3600");            //if (HttpContext.Current.Request.HttpMethod == "OPTIONS")            //{            //     HttpContext.Current.Response.End();            //}        }    }

  

  public class EbB : IEndpointBehavior      {        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)        {           // throw new NotImplementedException();        }        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)        {           // throw new NotImplementedException();        }        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)        {            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MessageInspector());           // throw new NotImplementedException();        }        public void Validate(ServiceEndpoint endpoint)        {           // throw new NotImplementedException();        }    }

  

WebHttpBinding wb = new WebHttpBinding();            sh.AddServiceEndpoint(typeof(IService1), wb, "http://localhost/xx1100");                       // EnableCrossOriginResourceSharingBehavior crossOriginBehavior = new EnableCrossOriginResourceSharingBehavior();                   foreach (var endpont in sh.Description.Endpoints)            {             endpont.Behaviors.Add(new EbB());               // endpont.Behaviors.Add(crossOriginBehavior);            }            sh.Open();            button1.Text = "已启动";

  

  //[OperationContract]        //[WebInvoke(UriTemplate = "poststr1",Method="POST",ResponseFormat=WebMessageFormat.Json)]        //string poststr1(csinfo cs);        [OperationContract]        [WebInvoke(UriTemplate = "poststr1",Method="*", ResponseFormat = WebMessageFormat.Json)]        string poststr1(csinfo cs);        //[OperationContract]        //[WebInvoke(UriTemplate = "poststr1", Method = "OPTIONS")]        //void poststr12();

  

   //void IService1.poststr12()        //{        //    string STR = "DDD";        //   // throw new NotImplementedException();        //}        public string poststr1(csinfo cs)        {            //if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")            //{            //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");            //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST");            //    WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");            //    WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();            //    string ff = "";            //    return null;            //}            //WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");             if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")            {                //  reply.Close();                return "";            }            if (cs == null)            {                return Xtools.json_TtoJson("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));            }            else            {                //OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("", ""));                var x = new { x = "poststr1的调用", a = "服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  A值:" + cs.a, b = "B值: " + cs.b, c = "C值:" + cs.c };                return Xtools.json_TtoJson(x);            }        }        public string poststr2(string cs)        {            var x = new { x = "poststr2的调用", a = "poststr2调用 服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "  cs值:" + cs };            return Xtools.json_TtoJson(x);        }    }

  

WCF header 域