2014-05-14 20 views
1

我有一个WCF服务,在提供正确的凭据时按预期工作。Windows Phone错误处理:使用BasicHttpBinding和TransportWithMessageCredential的WCF

当我试着使用错误的凭据消费服务,该服务将如预期MessageSecurityException错误,我收到一个错误:"MessageSecurityException was unhandled by user code"

我不知道如何处理这个异常,因为它是在Reference.cs文件是自动生成的,我的控制之下没有真正提出:

References.cs

public string EndLogin(System.IAsyncResult result) { 
    object[] _args = new object[0]; 
    string _result = ((string)(base.EndInvoke("Login", _args, result))); //Here is the error raised 
    return _result; 
} 

理想将检查service是否已接受凭据而不是依靠提出的错误,但不知道如何检查此问题。

希望有人能帮助我,让我的应用程序没有崩溃每个错误的登录;)

的Web.config:服务:

<?xml version="1.0"?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 

    <services> 
     <service name="BiBasicService.SalesMarketingService"> 
     <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" 
      contract="BiBasicService.ISalesMarketingService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <bindings> 
     <basicHttpBinding> 
     <binding name="basicHttpBinding"> 
      <security mode="TransportWithMessageCredential"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" /> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 

      <!-- To enable custom Role validation --> 
      <serviceAuthorization principalPermissionMode="Custom"> 
      <authorizationPolicies> 
       <add policyType="BiBasicService.Security.AuthorizationPolicy, BiBasicService" /> 
      </authorizationPolicies> 
      </serviceAuthorization> 

      <!-- To enable custom Username and Password validator--> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="BiBasicService.Security.CustomValidator, BiBasicService"/> 
      </serviceCredentials> 

     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

ServiceReferences.ClientConfig:客户:

<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_ISalesMarketingService" maxBufferSize="2147483647" 
        maxReceivedMessageSize="2147483647"> 
        <security mode="TransportWithMessageCredential" /> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="https://PUBLICDOMAIN/BasicHttp/SalesMarketingService.svc" 
       binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISalesMarketingService" 
       contract="ServiceReference1.ISalesMarketingService" name="BasicHttpBinding_ISalesMarketingService" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

回答

0

MessageSecurityException:这是一个绑定错误。

确保server sideclient side上的binding configuration必须匹配。 请张贴server的web.configclient的web.config

0

你可能想看看进入IErrorHandler接口,这样可以让你以更“全球层面”处理异常。 IErrorHandler是一个扩展,允许在抛出异常时显式控制应用程序的行为,实现IErrorHandler接口并将其添加到Dispatcher的ErrorHandlers属性中。 IErrorHandler使您能够显式控制生成的SOAP错误,决定是否将其发回给客户端,并执行相关任务(如日志记录)。错误处理程序按其添加到ErrorHandlers属性的顺序调用。

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx
http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/07/wcf-extensibility-ierrorhandler.aspx

相关问题