Could not add Access-Control-Allow-Origin to my WCF library Project

后端 未结 6 1131
Happy的楠姐
Happy的楠姐 2021-02-09 08:53

I\'m trying to understand why this ajax called doesn\'t work

 $.ajax({
        type: \'GET\',
        url: \"http://localhost:8732/Design_Time_Addresses/InMotion         


        
6条回答
  •  轮回少年
    2021-02-09 09:53

    For WCF service you have to develop new behavior and include it in the endpoint configuration:

    1. Create Message Inspector

          public class CustomHeaderMessageInspector : IDispatchMessageInspector
          {
              Dictionary requiredHeaders;
              public CustomHeaderMessageInspector (Dictionary headers)
              {
                  requiredHeaders = headers ?? new Dictionary();
              }
      
              public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
              {
                  return null;
              }
      
              public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
              {
                  var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
                  foreach (var item in requiredHeaders)
                  {
                      httpHeader.Headers.Add(item.Key, item.Value);
                  }           
              }
          }
      
    2. Create Endpoint Behavior and use Message Inspector to add headers

          public class EnableCrossOriginResourceSharingBehavior : BehaviorExtensionElement, IEndpointBehavior
          {
              public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
              {
      
              }
      
              public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
              {
      
              }
      
              public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
              {
                  var requiredHeaders = new Dictionary();
      
                  requiredHeaders.Add("Access-Control-Allow-Origin", "*");
                  requiredHeaders.Add("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS");
                  requiredHeaders.Add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
      
                  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CustomHeaderMessageInspector(requiredHeaders));
              }
      
              public void Validate(ServiceEndpoint endpoint)
              {
      
              }
      
              public override Type BehaviorType
              {
                  get { return typeof(EnableCrossOriginResourceSharingBehavior); }
              }
      
              protected override object CreateBehavior()
              {
                  return new EnableCrossOriginResourceSharingBehavior();
              }
          }
      
    3. Register new behavior in web.config

      
               
                          
              
      
      
    4. Add new behavior to endpoint behavior configuration

            
       
        
         
       
      
      
    5. Configure endpoint

      
      

提交回复
热议问题