Simple WCF Service Error while Executing

Ramu picture Ramu · Jun 10, 2014 · Viewed 14k times · Source

This may be very basic question in WCF. I'm new to it and not sure how to solve this please.

I have taken the WCF Service application template from visual studio and here is the code below what i have

**Interface: IService1.cs **

using System.ServiceModel;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }
}

** Service File Name : Service1.svc **

using System.ServiceModel;
namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

and i have edited the web.config file as

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehaviour" name="WcfService1.IService1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>

and the Error i get is: Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.

in Detail:

Error: Cannot obtain Metadata from http://localhost:59790/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:59790/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:59790/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:59790/Service1.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:59790/Service1.svc    The HTML document does not contain Web service discovery information.

Original Webconfig file as generated by Visual Studio

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>

I am sure this got to be simple configuration file problem. Not sure how to figure it out. Thanks for your help in advance.

Modifications to Web.config to add netTcpBinding Binding, which throws error in WCF testClient:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
        <endpoint address="Service1" binding="netTcpBinding" contract="WcfService1.IService1"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8090/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration><br>------------------------------------------------------------------


Project 2 : BasicHttp and NetTcp Binding hosted in Console App

I have the below web.config file(Just copied again, so that we can have all under one glance)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService"  behaviorConfiguration="mexBehaviour">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:59084/"/>
            <add baseAddress="net.tcp://localhost:59076/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

and Console App code for hosting

using System.ServiceModel;

namespace HelloServiceHost
{
    class Program
    {
        static void Main()
        {
            using(ServiceHost host  = new ServiceHost(typeof(HelloService.HelloService)))
            {
                host.Open();
                Console.WriteLine("Host Started");
                Console.ReadLine();
            }
        }
    }
}

Error : I'm getting error at host.Open() method where it says,

 HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
  • I tried other port number like 53895, thinking port 8080 might be pre occupied. No Luck!!
  • When i browsed for this error, i came to know this problem due to my account being non admin. Now my doubt is WCFTest Client also execued under my account itself. How can it run the similar code and i can't?
  • Also, any suggestions to make this work would be appriciated. May be something to do with Webconfig again??

Class that extends Interface

using System.ServiceModel;
namespace HelloService
{
    public class HelloService : IHelloService
    {
        public string GetMessage(string Name)
        {
            return "Hello " + Name;
        }
    }
}

Interface :

namespace HelloService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        String GetMessage(String Name);
    }
}

Thanks in advance!!

Answer

Aditya Patil picture Aditya Patil · Jun 10, 2014

Everything in the web.config looks okay with one exception: The "name" in the line below should be "WcfService1.Service1" and not "WcfService1.IService1":

<service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">

My web configuration is as follows and it works for me:

<?xml version="1.0"?>
<configuration>    
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>  
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>