0

i created a WCF webservice in .net/c#. I also created a test client app. everything works fine. Now i wanted to add a custom exception

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

    }
}

it gets thrown here:

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

everything alright up until here.

Now i want to add the FaultContract to the Interface:

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

And now when i try to update the service reference at my client app after adding the [FaultContract] to the interface i get this error:

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'...

Why does adding the [FaultContract] create a mismatch and what do i need to configure to solve this problem? Is there something wrong with the FaultContract or is it merely a problem with the configuration? (The second would make me wonder why it works without the FaultContract)

I am using .NET 4.5.

here my 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>

and my applications 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>
coconut
  • 494
  • 2
  • 5
  • 13
  • Instead of updating service reference, try to remove and add it again. Make sure the service http://localhost:51104/MyService.svc?wsdl is available when accessed through browser – Consult Yarla Mar 30 '15 at 15:38
  • Try removing the inheritance from Exception. Your MyException class is actually the details for the FaultException. Also do a rebuild before updating the service reference. – Stephen Wilson Mar 30 '15 at 16:05
  • Thx for the answers - after removing the inheritance i could access localhost:51104/MyService.svc?wsdl. There is no more mismatching error now. The problem now is although i did a try/catch block at the client it says at the webservice code side "FaultException was unhandled by user" – coconut Mar 30 '15 at 16:48
  • It turns out i had to untick the following two boxes in my VS -> Tools -> Options -> Debugging -> General: "Enable the Exception Assistant" and "Enable Just My Code". Problem is now SOLVED. – coconut Apr 02 '15 at 16:19

0 Answers0