2013-07-02 60 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);