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 =

  

时间: 2024-10-05 04:28:50

wcf Origin的相关文章

WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题

摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication, 但是仍然出现IIS没有启用Windows Authentication的问题. 在网络上能查到的资料很少. 通过自己的troubleshooting发现所遇到的错误提示比较具有迷惑性. 所以POST上来给大家分享一下. 问题 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Servic

MSCRM 通过Ajax调用WCF服务

Call WCF Service from Dynamics CRM using AJAX A couple of days back, I had one of my ex-colleagues call me regarding a problem he had been facing when making jQuery AJAX calls to JSON WCF Services from Dynamics CRM. I had encountered this same proble

wcf综合运用之:大文件异步断点续传

在WCF下作大文件的上传,首先想到使用的就是Stream,这也是微软推荐的使用方式.处理流程是:首先把文件加载到内存中,加载完毕后传递数据.这种处理方式对小文件,值得推荐,比如几K,几十k的图片文件,文本文件对大文件就不适用,比如10G的电影,把10G的数据加载到缓存中再传递,这是不可想象的.这个时候我们想到的就是断点续传.由于数据量很大.会导致当前程序阻塞,所以采用异步发送的方式,以进度条显示出来,这也是本篇文章所要实现的功能. 另外,目前BasicHttpBinding, NetTcpBin

WCF SOA --- AJAX 跨域请求处理 CORS for WCF

一.问题        跨域请求无法处理的问题,由于为了阻止恶意的网站通过JS脚本来窃取正常网站受保护的资源.所由所有的浏览器的默认策略是阻止XmlHttpRequest的跨域的异步请求. 但是对于一个 复合型的应用集合来说,可能需要使用不同的域来部署我们的应用.对于这种正常的需求,我们的服务与应用就需要能够支持指定信认域的跨域的异步请法.       通常来说,我们有三种替代方案       1, 使用JSONP,             JSONP(JSON with Padding), 但

WCF跨域调用

1  WCf跨域调用  一个用 Jsonp  (这是蒋金楠大神写的一个实例这里把他拷贝过来) 在契约中 [ServiceContract] public interface IEmployees { [WebGet(UriTemplate = "all",ResponseFormat =WebMessageFormat.Json)] IEnumerable<Employee> GetAll(); } public class Employee { public string

WCF The service cannot be activated because it does not support ASP.NET compatibility

测试发布到虚拟目录中时发生的报错现象,由于不支持asp.net兼容性而导致服务无法激活启用. 具体错误信息如下: Server Error in '/Service2' Application. -------------------------------------------------------------------------------- The service cannot be activated because it does not support ASP.NET com

Restful风格wcf调用2——增删改查

写在前面 上篇文章介绍如何将wcf项目,修改成restful风格的接口,并在上面提供了查询的功能,上篇文章中也感谢园友在评论中的提的建议,自己也思考了下,确实是那个道理.在urltemplate中,定义的url确实不规范,虽然能实现功能,但是缺少点专业性.rest风格的请求,是通过post,delete,get,put等请求方法来区别的,而不是通过在url中取不同的名字来进行区别.这里再次感谢@~Js园友提醒. 在这篇文章中将最新的代码贴出来,方便查看. 系列文章 Restful风格wcf调用 

JQuery Ajax调用WCF实例以及遇到的问题

1.遇到的最多的问题就是跨域问题,这个时间需要我们添加如下代码解决跨域的问题 第一步:在服务类加Attribute [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 第二步:在构造函数中添加 if (WebOperationContext.Current != null) { WebOperationContext.Current.OutgoingRe

.NET4.5中WCF中默认生成的basicHttpsBinding的研究

起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHttpBinding. 2.因为需要单次传递大批量数据,所以修改绑定配置,如下: <basicHttpBinding> <binding name="BasicHttpBinding_IService" closeTimeout="00:10:00" r