Lock
Lock

Reputation: 5522

wcf service not working- failed to add service

I am trying to make my first WCF service. I am having issues- I have written a struct, a class and a few methods. My service contract looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WebshopReports
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReports" in both code and config file together.
    [ServiceContract]
    public interface IReports
    {
        [OperationContract]
        List<Category> getReportCategories(string dir);

        [OperationContract]
        List<CReport> getReportList(Category category);

    }
}

When I run it using the test client in visual studio, I get the following:

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

I am sure it has something to do with me web.config file. It is below. Can someone please help me as i am unsure how to configure the web.config:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata 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"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Weird thing is if I change the CReport from a class to a struct, it will work.

Upvotes: 2

Views: 1759

Answers (5)

lei.liu
lei.liu

Reputation: 9

Look at my web.config, I think your config file is wrong:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="NoneSecurity"
          maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
          <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
          <security mode="None"/>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="Elong.GlobalHotel.Host.IHotelAPIBehavior"
        name="Elong.GlobalHotel.Host.IHotelAPI">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="NoneSecurity" contract="Elong.GlobalHotel.Host.IIHotelAPI">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Elong.GlobalHotel.Host.IHotelAPIBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Upvotes: 1

Tabish Sarwar
Tabish Sarwar

Reputation: 1525

If i am not mistaken . i cant find any Service Node in your configuration. YOu should inlude something like this in your configuration file

<configuration>
      <System.ServiceModel>
       <Services>
        <Service name="YourService.Somthing">
        <EndPoint name="default" address="" binding="basicHttpBinding" bindingConfiguration="" contract="YourContrat.Something"></Endpoint>
        <endpoint kind="mexEndpoint" address="/mex" />
        </Service>
       </Services>
<System.ServiceModel>
</configuration>

Hope this helps.

Thanks

Upvotes: 0

Russell
Russell

Reputation: 17719

Double check that you have marked CReport as Serializable. This will allow the WCF Serializer to expose the class definition in the WCF metadata.

Upvotes: 0

competent_tech
competent_tech

Reputation: 44921

In our experience, there are two main causes of this type of failure:

1) Serialization on the server side. This could be due to classes that are not serializable or attempts to serialize abstract classes without using the KnownType attribute.

2) Serialization on the client side due to WCF's confusion between multiple Collection or Dictionary type classes that have the same generic signature.

Based on your description, it sounds like item 1 is your issue.

The best way to troubleshoot WCF issues like this is to add the following block to the configuration section of your web.config file:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="traceListener"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData="c:\log\WebTrace.svclog"  />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

Once you try to use the interface and it fails, double-click on the svclog file and that will open WCF's log viewer tool. Any exceptions that occur will be highlighted in red and you can drill down into them to see the specific issue that is causing the WCF service to fail.

Upvotes: 1

Mike Atlas
Mike Atlas

Reputation: 8231

You need to ensure sure that CReport is serializable, otherwise it would be impossible for WCF to create metadata to exchange (via WSDL in your case) about your service. Read up on adding the DataContract attribute to your CReport class.

Upvotes: 0

Related Questions