0

.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 に何か問題がありますか、それとも単に構成の問題ですか? (2番目は、なぜ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