2014-02-27 143 views
0

我今天转换了我的WCF服务以使用WSHttpBinding而不是BasicHttpBinding。它仍处于开发阶段,因此我正在使用自签名证书。我遵循的示例位于Here默认对象值

终于让代码运行起来,如示例所示(请参阅可下载的代码中的配置示例),我决定继续使用像我一样的Channel Factories之前。

现在,当我调用WCF方法时,我可以清楚地看到,我发送的对象是使用期望值填充的,但是如果我进入WCF端,这些值是它们的默认值。例如Guid的将是Empty Guid's,而int将是0.总是。

任何想法可能会造成这种情况?这里是我的一些代码:

在web.config:

<add key="ClientCertificate" value="WcfClient" /> 
<add key="ServerCertificate" value="WcfServer" /> 

<system.serviceModel> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="CustomBehavior"> 
      <clientCredentials> 
      <clientCertificate findValue="WcfClient" 
           x509FindType="FindBySubjectName" 
           storeLocation="CurrentUser" 
           storeName="My" /> 
      <serviceCertificate> 
       <authentication certificateValidationMode="PeerTrust"/> 
      </serviceCertificate> 
      </clientCredentials> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="WSHttpBinding_IDocumentsService" closeTimeout="00:10:00" 
      openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferPoolSize="2147483647" 
      maxReceivedMessageSize="2147483647" messageEncoding="Mtom" allowCookies="true"> 
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
      maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
      <security mode="Message"> 
       <transport clientCredentialType="Windows" proxyCredentialType="None" 
        realm="" /> 
       <message clientCredentialType="Certificate" negotiateServiceCredential="true" 
        algorithmSuite="Default" establishSecurityContext="true" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:58790/DocumentsService.svc" 
       binding="wsHttpBinding" 
       bindingConfiguration="WSHttpBinding_IDocumentsService" 
       contract="DocumentsService.IDocumentsService" 
       name="WSHttpBinding_IDocumentsService" 
       behaviorConfiguration="CustomBehavior"> 
     <identity> 
      <dns value="WcfServer" /> 
     </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

这是我的通道工厂

public static class ServiceObjects 
{ 
    public static IDocumentsService DocumentsSVC { get { return GetDocServiceClient(); } } 


    #region Private Members 
    private static WSHttpBinding _DMBinding = new WSHttpBinding("WSHttpBinding_IDocumentsService"); 
    private static EndpointIdentity _DMIdentity = EndpointIdentity.CreateDnsIdentity(ConfigurationManager.AppSettings.Get("ServerCertificate")); 
    private static EndpointAddress _DMEndpoint = new EndpointAddress(new Uri(ConfigurationManager.AppSettings.Get("DocumentsService")), _DMIdentity); 


    private static IDocumentsService GetDocServiceClient() 
    { 
     ChannelFactory<IDocumentsService> _docSvcFactory = new ChannelFactory<IDocumentsService>(_DMBinding, _DMEndpoint); 

     _docSvcFactory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerOrChainTrust; 

     _docSvcFactory.Credentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings.Get("ClientCertificate")); 
     _docSvcFactory.Credentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindBySubjectName, ConfigurationManager.AppSettings.Get("ServerCertificate")); 


     return _docSvcFactory.CreateChannel(); 
    } 
    #endregion 
} 

当我调用服务,在例如客户端:

private static Guid _UserID = (HttpContext.Current.User as Titan.Web.Classes.Identity.CustomPrincipal).UserId; 

ServiceObjects.DocumentsSVC.GetDocumentsByFolderID(new DocumentRequest { CurrentUserID = _UserID }) 

我可以看到_UserID被填充,但在服务器端不是。

这是我服务的config

<system.serviceModel> 
<bindings> 
    <wsHttpBinding> 
    <binding name="wsHttpEndpointBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="true" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Mtom"> 
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
     <security> 
     <message clientCredentialType="Certificate" /> 
     </security> 
    </binding> 
    </wsHttpBinding> 
</bindings> 
<services> 
    <service name="Titan.WCF.Documents.DocumentsService" behaviorConfiguration="DocumentsServiceBehavior"> 
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" contract="Titan.WCF.Documents.IDocumentsService"> 
     <!-- 
      Upon deployment, the following identity element should be removed or replaced to reflect the 
      identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
      automatically. 
     --> 
     <identity> 
     <dns value="localhost"/> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="DocumentsServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <dataContractSerializer maxItemsInObjectGraph="2147483647" /> 
     <serviceCredentials> 
     <clientCertificate> 
      <!-- Remove the NoCheck in production, this is only for when we use a self signed cert --> 
      <authentication certificateValidationMode="PeerOrChainTrust" revocationMode="NoCheck" /> 
     </clientCertificate> 
     <serviceCertificate findValue="WCfServer" 
      storeLocation="CurrentUser" 
      storeName="My" 
      x509FindType="FindBySubjectName" /> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
</system.serviceModel> 

回答

0

我不知道为什么这会有所作为,但它做到了。

我需要更新我的服务参考。

我猜想出现了一个菜鸟错误,但如果我做的唯一一件事是改变绑定,端点等,为什么会有所作为呢?

+0

@dhrumilap将不会收到您的回复通知,除非您将其作为评论发布给他的回答。 –

0

看来问题就在于你用下面的代码使该服务的调用方式:

ServiceObjects.DocumentsSVC.GetDocumentsByFolderID(new DocumentRequest { CurrentUserID = _UserID }); 

这里你试图调用该服务功能不需要创建服务的代理对象。那是因为你在服务中写了一个静态类。

在WCF服务中,您不能使用静态类。您需要创建类的实例(服务代理对象),然后调用服务功能。

+0

Hi @dhrumilap。不幸的是,这并没有成功。过去两年我一直使用静态方法运行代码。我们最近才到了需要我们服务安全性的地步,才将它部署到一个测试网站,以提高安全性。请检查我的答案,我找到了解决方案。 – Ebbs

+0

@MrThursday,我的坏! – dhrumilap

+0

哈哈不是问题:) – Ebbs

1

当然,由于您在服务中更改了配置,因此您需要更新服务参考,以便客户端也可以更新其配置。除非您这样做,否则客户端将继续使用旧配置调用服务,该配置会在配置文件中读取配置文件时使用新配置设置运行。

它可能很累人,但它是这样的。