0

我在 .net/c# 中创建了一个 WCF 网络服务。我还创建了一个测试客户端应用程序。一切正常。现在我想添加一个自定义异常

[DataContract]
[Serializable]
public class MyException : Exception
{    
    public MyException(string message)
        : base()
    {

    }
}

它被扔在这里:

if{ dosomth(); }
else{ 
   MyException mE = new MyException("my error occured");
   throw new FaultException<myException>(mE); 
}

一切都好,直到这里。

现在我想将 FaultContract 添加到接口:

[OperationContract]
[FaultContract(typeof(MyException))]
Double myMethod(String x, Double y, String z);

现在,当我在将 [FaultContract] 添加到界面后尝试在客户端应用程序上更新服务引用时,我收到此错误:

There was an error downloading 'http://localhost:51104/MyService.svc/_vti_bin/ListData.svc/$metadata'.
The request failed with HTTP status 400:Bad Request.
Metadata contains a reference that cannot be resolved:
'http://localhost:51104/MyService.svc'.
Content Type application/soap+xml; charset=utf-8 was not supported by service 'http://localhost:51104/MyService.svc'. The client and service bindings my be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'...

为什么添加 [FaultContract] 会造成不匹配,我需要配置什么来解决这个问题?FaultContract 有问题还是仅仅是配置问题?(第二个会让我想知道为什么它在没有故障合同的情况下工作)

我正在使用.NET 4.5。

这是我的 web.config:

    <?xml version="1.0"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <!--
        For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

        The following attributes can be set on the <httpRuntime> tag.
          <system.Web>
            <httpRuntime targetFramework="4.5" />
          </system.Web>
      -->
      <system.web>
        <compilation debug="true" targetFramework="4.5"/>
        <httpRuntime/>
        <pages controlRenderingCompatibilityVersion="4.0"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
      </system.serviceModel>
    </configuration>

和我的应用程序 App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IMyService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:51104/MyService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
                contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
        </client>
    </system.serviceModel>
</configuration>
4

0 回答 0