我在.net / c#中创建了一个WCF Web服务。我还创建了一个测试客户端应用程序一切正常。 现在我想添加一个自定义异常
[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有什么问题,还是仅仅是配置问题? (第二个会让我想知道为什么没有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>