Windows服务调用WCF服务(web http配置)

本文关键字:服务 http 配置 web 调用 WCF Windows | 更新日期: 2023-09-27 18:20:51

我已经在这里搜索了所有可用的问题,我不知道这个问题是我的配置特有的,还是我遗漏了什么。

背景:

我有一个WCF服务托管在IIS 7上,使用HTTPS绑定。该服务用于发送/接收大型对象。一个windows服务使用这个WCF服务将这些大对象发送到它,但我收到了400错误请求错误。我在服务中启用了跟踪,错误为"已超过传入消息的最大消息大小配额(65536)"。以下是WCF和Windows服务配置以及如何使用WCF服务。

WCF服务配置。

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <globalization culture="mk-MK" fileEncoding="windows-1251" />
</system.web>
<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="basicWebHttp" allowCookies="true" closeTimeout="00:59:59" receiveTimeout="00:59:59"
             sendTimeout="00:59:59" maxReceivedMessageSize="2147483647" 
             maxBufferSize="2147483647" maxBufferPoolSize="2147483647" >
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport clientCredentialType="None" realm="" proxyCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceTimeouts transactionTimeout="00:10:00"/>
      <serviceThrottling maxConcurrentCalls="20" maxConcurrentSessions="20" maxConcurrentInstances="20" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttp">
      <webHttp automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
    contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<defaultDocument>
  <files>
    <remove value="default.aspx" />
    <remove value="iisstart.htm" />
    <remove value="index.html" />
    <remove value="index.htm" />
    <remove value="Default.asp" />
    <remove value="Default.htm" />
    <remove value="Default.html" />
    <add value="VisService.svc" />
  </files>
</defaultDocument>
<urlCompression doDynamicCompression="false" />
</system.webServer>
<system.diagnostics>
<sources>
  <source name="System.ServiceModel"
          switchValue="Information, ActivityTracing, Error"
          propagateActivity="true">
    <listeners>
      <add name="traceListener"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData= "c:'log'Traces.svclog" />
    </listeners>
  </source>
</sources>
</system.diagnostics>

该服务通过具有以下配置的Windows服务使用:

<system.web>
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="basicWebHttp" allowCookies="true" closeTimeout="00:59:59" receiveTimeout="00:59:59"
              sendTimeout="00:59:59"
              maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="basicWebHttpBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="" behaviorConfiguration="basicWebHttpBehaviour"
            binding="webHttpBinding" bindingConfiguration="basicWebHttp"
            contract="labis.IVisService" name="BasicHttpBinding_IVisService" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

该服务的名称如下:

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };
WebChannelFactory<IVisService> factory = new WebChannelFactory<IVisService>(
                                "BasicHttpBinding_IVisService",
                                new Uri("https://some_ip/service.svc"));
IVisService service = factory.CreateChannel();
service.PostData(large_object);

我已经启用了跟踪,从日志中我可以看到服务器抛出了异常:已超过传入消息的最大消息大小配额(65536)

我想我已经设置了所有需要的属性,但没有运气。此外,在IIS配置编辑器中,我已将system.webServer/security/requestFiltering-requestLimits属性设置为最大

有什么帮助吗?

谢谢:)

编辑1

我粘贴了错误的端点配置元素。最初的版本缺少behaviorConfiguration="WebHttp"部分,但我已经用这个属性对它进行了测试。

Windows服务调用WCF服务(web http配置)

在尝试了所有的可能性之后,解决方案非常简单。我向WCF服务添加了以下配置

<protocolMapping>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="basicWebHttp" />
  <add scheme="http" binding="webHttpBinding" bindingConfiguration="basicWebHttp" />
</protocolMapping>

点击此处了解更多信息http://msdn.microsoft.com/en-us/library/ee816881.aspx

我想这是有效的,因为服务使用HTTPS绑定,所以它需要绑定的显式映射。

至少这对我有效

在WCF服务配置中

您忘记将行为配置(您将其命名为"WebHttp")添加到端点

试试这个:

<endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
    contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />
<system.web>
<httpRuntime maxRequestLength="2147483647" /  >
</system.web>

将上面的conf放入服务所在的web.config中。

您可以尝试以下操作:

请尝试在WebChannelFactory的构造函数中提供端点。

但首先,您必须提取配置并创建端点的实例,然后手动提供行为。

http://weblogs.asp.net/cibrax/archive/2010/05/11/getting-wcf-bindings-and-behaviors-from-any-config-source.aspx是关于如何做到这一点的一个很好的资源。

public Binding ResolveBinding(string name)
    {
    Configuration appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
      BindingsSection section = serviceModel.Bindings;
      foreach (var bindingCollection in section.BindingCollections)
      {
        if (bindingCollection.ConfiguredBindings.Count > 0 
    && bindingCollection.ConfiguredBindings[0].Name == name)
        {
          var bindingElement = bindingCollection.ConfiguredBindings[0];
          var binding = (Binding)Activator.CreateInstance(bindingCollection.BindingType);
          binding.Name = bindingElement.Name;
          bindingElement.ApplyConfiguration(binding);
          return binding;
        }
      }
      return null;
    }
public List<IEndpointBehavior> ResolveEndpointBehavior(string name)
{
     Configuration appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
      BindingsSection section = serviceModel.Behaviors;
  List<IEndpointBehavior> endpointBehaviors = new List<IEndpointBehavior>();
  if (section.EndpointBehaviors.Count > 0 
&& section.EndpointBehaviors[0].Name == name)
  {
    var behaviorCollectionElement = section.EndpointBehaviors[0];
    foreach (BehaviorExtensionElement behaviorExtension in behaviorCollectionElement)
    {
      object extension = behaviorExtension.GetType().InvokeMember("CreateBehavior",
            BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
            null, behaviorExtension, null);
      endpointBehaviors.Add((IEndpointBehavior)extension);
    }
   return endpointBehaviors;
 }
 return null;
}

然后使用:

    var binding = ResolveBinding("MyBinding");
    var behaviors = ResolveEndpointBehavior("MyBehavior");
    System.ServiceModel.Description.ContractDescription contract = System.ServiceModel.Description.ContractDescription.GetContract(typeof(IVisService));
     System.ServiceModel.Description.ServiceEndpoint ep = new System.ServiceModel.Description.ServiceEndpoint(contract, binding, "https://some_ip/service.svc");
                    foreach (var behavior in behaviors)
                    {
                        ep.Behaviors.Add(behavior);
                    }
System.ServiceModel.Web.WebChannelFactory<IVisService> t = new System.ServiceModel.Web.WebChannelFactory<IVisService>(ep);

在我面临同样问题之前的最后两周,即使我在服务端和客户端都更新了配置,与您的配置相同,但代码不起作用,我们也收到了相同的超大小消息。

然后我们执行了以下操作

1. Updated the webconfig file of service
2. Reset IIS of service
3. Created a proxy for service
4. updated client web.config file
5. reset the iis of client(webapplication)

然后它对我起了作用。