wcf Origin

  WebHttpBinding bd = new WebHttpBinding();

            //WebServiceHost sh = new WebServiceHost(typeof(Bl_x), new Uri("http://localhost/xxii"));
            //ServiceHost sh = new ServiceHost(typeof(Bl_x), new Uri("http://localhost/xxii"));

            ServiceHost sh = new ServiceHost(typeof(Bl_x));
            sh.AddServiceEndpoint(typeof(JK.IXX), bd, new Uri("http://localhost/xxii"));
            foreach (var item in sh.Description.Endpoints)
            {
                WebHttpBehavior tmp = new WebHttpBehavior();
                tmp.HelpEnabled = true;
                WebHttpBehavior dds = item.Behaviors.Find<WebHttpBehavior>();
                if (dds != null)
                {
                    dds.HelpEnabled = true;
                }
                else
                {
                    item.Behaviors.Add(tmp);
                }
                //item.Behaviors.Add(wb);
            }
            sh.Open();
            button1.Text = "已启动";
            button1.Enabled = false;

  

  WebHttpBinding bd = new WebHttpBinding();

            ChannelFactory<JK.IXX> dd = new ChannelFactory<JK.IXX>(bd, "http://localhost/xxii");
            dd.Endpoint.Behaviors.Add(new WebHttpBehavior());
            MessageBox.Show(dd.CreateChannel().getstr());

  

    [Record]
        [WebInvoke(UriTemplate = "poststr1", Method = "*", ResponseFormat = WebMessageFormat.Json)]
        public string poststr1(csinfo cs)
        {
             var channelDispatcher = null as ChannelDispatcher;
          if (channelDispatcher != null)

             // channelDispatcher.ErrorHandlers.Add(IErrorHandler);
              
            //if (string.Equals("json", format, StringComparison.OrdinalIgnoreCase))
            //{
            //    if (WebOperationContext.Current != null)
            //        WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
            //}

            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
            //if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
            //{
  
            //    WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound();
            //    string ff = "";
            //    return null;
            //}
            //WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); 

            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET,POST");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");

            OperationContext dd = OperationContext.Current;

            if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
            {
               
                //  reply.Close();
              //  return "";
            }
          

            if (cs == null)
            {
                
                return Xtools.json_TtoJson(new { x = "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"), y = "dd" });
            }
            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 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)
        {
            new string('*',55);
            
            WebOperationContext.Current.OutgoingResponse.Headers.Clear();
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Server", "44441");
         
            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")
            {
        //WebOperationContext.Current.OutgoingResponse.StatusCode = 

  

原文地址:https://www.cnblogs.com/xiangxiong/p/6797017.html