调用WCF服务的Windows服务(Web http配置)

时间:2013-07-02 11:40:38

标签: c# .net wcf windows-services wcf-binding

我在这里搜索了所有可用的问题,我不知道问题是否与我的配置有关,或者我错过了什么。

背景

我使用HTTPS绑定在IIS 7上托管了WCF服务。该服务用于发送/接收大对象。 Windows服务使用此WCF服务将这些大对象发送到它,但我收到400 Bad Request错误。我已在服务中启用跟踪,错误是“已超出传入邮件的最大邮件大小配额(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”部分,但我已经使用这个属性进行了测试。

5 个答案:

答案 0 :(得分:2)

在尝试了所有可能性后,解决方案非常简单。 我将以下配置添加到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绑定,因此需要绑定的显式映射。

至少这对我有用

答案 1 :(得分:1)

在您的WCF服务配置

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

试试这个:

<endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
    contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />

答案 2 :(得分:1)

<system.web>
<httpRuntime maxRequestLength="2147483647" /  >
</system.web>

将上述内容放入托管服务的web.config中。

答案 3 :(得分:1)

您可以尝试这样的事情:

尝试在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);

答案 4 :(得分:0)

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

然后我们执行了以下操作

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)
然后它对我有用。

相关问题