2012-09-17 73 views
1

的我已经在这里得到了快速概述:WCF服务的服务呼叫流端点上的第三个呼叫失败

WCF SOAP服务正在运行通过HTTPS类型证书的信息凭证。我使用了2个端点(除了mex),其中1个用于我的普通服务调用,另一个用于流式文件。这里是我的服务(编辑项目的一些名)的web.config:

<bindings> 
    <wsHttpBinding> 
    <binding name="wsHttpEndpointBinding"> 
     <security mode="TransportWithMessageCredential"> 
     <message clientCredentialType="Certificate" /> 
     </security> 
    </binding> 
    </wsHttpBinding> 
    <basicHttpBinding> 
     <binding name="streamBinding" transferMode="Streamed" messageEncoding="Mtom" maxReceivedMessageSize="2147483646"> 
      <security mode="TransportWithMessageCredential"> 
       <message clientCredentialType="Certificate" /> 
      </security> 
     </binding> 
    </basicHttpBinding>   
</bindings> 
<services> 
    <service behaviorConfiguration="Services.Behavior" name="MyInterface"> 
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="wsHttpEndpoint" contract="IMyInterface" /> 
    <endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="streamBinding" name="streamEndpoint" contract="IMyInterface" /> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="Service.Behavior"> 
     <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <dataContractSerializer maxItemsInObjectGraph="2147483646" /> 
     <serviceCredentials> 
     <serviceCertificate findValue="CN=Server.Temp.Dev" /> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

服务有一个方法,DownloadDocument,我是测试。下面是签名:

DocumentDownloadResponse DownloadDocument(DocumentDownloadRequest要求);

值得注意的服务抛出异常时传递的数据是无效的,和DownloadDocument捕捉那些异常和传递回响应对象的错误消息。

的DocumentDownloadRequest看起来是这样的:

[MessageContract] 
public class DocumentDownloadRequest 
{ 
    [MessageHeader] 
    public string SecurityToken { get; set; } 

    [MessageHeader] 
    public string LoginName { get; set; } 

    [MessageHeader] 
    public string DocumentId { get; set; } 
} 

而且DownloadDocumentResponse:

[MessageContract] 
public class DocumentDownloadResponse : ServiceResponse<Stream> 
{ 
    public DocumentDownloadResponse() 
    { 
     Data = Stream.Null; 
    } 

    [MessageHeader(MustUnderstand = true)] 
    public bool Success { get; set; } 

    [MessageHeader(MustUnderstand = true)] 
    public string ErrorMessage { get; set; } 

    [MessageBodyMember(Order = 1)] 
    public Stream Data { get; set; } 
} 

以下是我把它从客户端:

 var soapServiceClient = new SoapServiceClient("streamEndpoint"); 
     bool success; 
     Stream stream; 
     string errorMessage = 
      soapServiceClient.DownloadDocument(documentId, loginName, securityToken, out success, out stream); 

     serviceClient.Close(); 

凡SecurityToken而LoginName是项需要验证。奇怪的是,从我的测试客户端,当我使用有效数据调用DownloadDocument时,我能够按照需要完美地下载文件。但是,如果我传递了无效的LoginName或SecurityToken,则会收到指示错误数据的错误消息(如预期的那样)。但是,如果我传递3次无效数据,则客户端会超时。在本地运行服务我没有遇到这个问题,everythign按预期运行。奇怪的是,当我开着小提琴手跑时,我不明白这个问题。当我使用我的开发服务器上的服务运行时,我遇到了问题。

开发服务器上的配置与我在本地运行的配置相匹配。使用SvcTraceTool我没有看到任何错误,只是它只记录前2次成功的调用,而不是失败的调用。这几乎让我觉得端点只是以某种方式关闭了自己。

悬崖:

1)2个终点,如果这是流之一(一个我很担心)服务。 2)可以使用流式端点调用方法下载带有效数据的文件 3)服务正确捕获不良数据2次,挂起第3次。 SvcTraceTool中没有日志,客户端超时。

任何想法?

感谢

+0

任何积极的结果? –

+1

是的,事实证明,在写入文件后,我没有正确关闭客户端的数据流。另外,在服务端发生错误的情况下,我返回Stream.Null,这也需要关闭。 – TheTFo

+0

很好,谢谢。但为了以防万一,你能告诉我关闭流的正确方式/顺序是什么? –

回答

2

为了回答罗德里戈,我想我会发布更多的细节:

首先,包装你生成的代理类的东西有点像这对正确处理错误:

public class ProxyWrapper<TServiceClientType, TResultType> 
    where TServiceClientType : class, ICommunicationObject 
{ 
    private static string _endPoint; 

    public ProxyWrapper(string endPoint = "") 
    { 
     _endPoint = endPoint; 
    } 

    public TResultType Wrap(Func<string, TServiceClientType> constructWithEndpoint, 
            Func<TServiceClientType, TResultType> codeBlock) 
    { 
     TResultType result = default(TResultType); 
     TServiceClientType client = default(TServiceClientType); 
     try 
     { 
      client = constructWithEndpoint(_endPoint); 
      result = codeBlock(client); 
      client.Close(); 
     } 
     catch (Exception) 
     { 
      if (client != null) 
      { 
       client.Abort(); 
      } 
      throw; 
     } 
     return result; 
    } 
} 

然后我有一个客户端类包装服务调用。这里的DownloadDocument方法:

public MyServiceResponse<Stream> DownloadDocument(string loginName, 
                  string documentId) 
    { 

     var proxyWrapper = new MyProxyWrapper<DocumentDownloadResponse>(StreamEndpoint); 
     DocumentDownloadResponse response = 
      proxyWrapper.Wrap((client) => 
      { 
       Stream data; 
       bool success; 
       string errorMessage = client.DownloadDocument(documentId, loginName, 
                   out success, 
                   out data); 
       return new DocumentDownloadResponse 
       { 
        Data = data, 
        Success = success, 
        ErrorMessage = errorMessage 
       }; 
      }); 

     var result = new MyServiceResponse<Stream> 
     { 
      Success = response.Success, 
      ErrorMessage = response.ErrorMessage 
     }; 

     if (!response.Success) 
     { 
      result.Data = null; 
      response.Data.Close(); 
     } 
     else 
     { 
      result.Data = response.Data; 
     } 

     return result; 
    } 

注:MyProxyWrapper从ProxyWrapper继承和指定的WCF客户端代理类。现在的实际调用看起来是这样的:

var myClient = new MyClient(); 
var downloadDocumentResponse = myClient.DownloadDocument(someId); 
           using (
            Stream output = 
             File.OpenWrite(someFilePath)) 
           { 
            downloadDocumentResponse.Data.CopyTo(output, 2048); 
            downloadDocumentResponse.Data.Close(); 
           } 

需要注意的是,我称之为.Close()在流,写入文件后一次,而一旦这两个地区是response.Success ==假。

+0

令人惊叹。非常感谢!我会看看流关闭。 –