2013-07-02 54 views
2

我已经在这里搜索了所有可用的问题,我不知道问题是特定于我的配置还是我错过了一些东西。Windows服务调用WCF服务(网页http配置)

背景:

我使用HTTPS绑定托管在IIS 7的WCF服务。该服务用于发送/接收大型对象。一个Windows服务使用这个WCF服务将这些大对象发送给它,但是我得到了400错误请求错误。我在服务中启用了跟踪,错误是“传入邮件的最大邮件大小限额(65536)已被超出”。以下是WCF和Windows服务配置以及如何使用wcf服务。

WCF服务配置。

<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.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.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”部分,但我已经测试它包含此属性。

回答

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

在你的WCF服务配置

你忘了添加行为的配置(你把它命名为 “WebHttp”)到终点

试试这个:之前我

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

我粘贴错误的配置。在我的配置中添加了这部分,并且错误仍然存​​在。 –

+0

你试过里程吗? 添加到web.config: <的httpRuntime的maxRequestLength = “2147483647”/>

+0

我尝试了,没有帮助。这是我尝试过的第一件事之一 –

0

过去2周面临同样的问题,即使我已经更新了服务和客户端的配置,与您的代码相同,但代码无效,我们得到了相同的超大小消息。

然后我们进行了以下操作

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) 

然后它为我工作。

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

将上面的配置文件放到web.config的服务托管位置。

+0

不,它是一样的错误 –

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);