码迷,mamicode.com
首页 > 其他好文 > 详细

wcf Origin

时间:2017-05-02 17:24:18      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:receive   button   spec   for   ring   policy   local   opera   show   

  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 = 

  

wcf Origin

标签:receive   button   spec   for   ring   policy   local   opera   show   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!