3

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!!

Ramu
  • 343
  • 1
  • 6
  • 21
  • How are you trying to use this service? Are you just accessing it from the URL to test, or are you calling it from another application? Also, how are you hosting the service (IIS, website, windows service, etc...)? The configuration may be correct, but if an instance of the service (called and endpoint) isn't started, this can cause issues. – xDaevax Jun 10 '14 at 15:45
  • Try changing the contract name in the config file to the fully qualified name - `WcfService1.IService1` and the name on the service to `WcfService1.Service1`. – Tim Jun 10 '14 at 15:46
  • @xDaevax Apologies i should said that. I have made Service1.svc as start up page and it opened in WCF Test Client. I have edited web.config file as show and when i revert back the changes in web.config file to normal as it is when it was created, its all working fine. – Ramu Jun 10 '14 at 15:49
  • It seems as though you have only configured the meta data service behavior, and not the actual service? Try adding an additional binding (similar to what is found here: http://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic5) and see if that helps. In particular, the code-listing is in listing 5.5 – xDaevax Jun 10 '14 at 15:59
  • @Tim : Thanks for your feedback mate. I have changed the code and edited my question accordingly, Still i am facing below error as mentioned above mate. – Ramu Jun 10 '14 at 16:03
  • XDaevax:i hve removed the behaviour and stil giving error mate. I have appended the contents of original web config file to my question. That also doesnt seems to have all the binding and other information, but still its working fine. Not sure how – Ramu Jun 10 '14 at 16:27

2 Answers2

4

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>
Uriil
  • 11,948
  • 11
  • 47
  • 68
Aditya Patil
  • 548
  • 4
  • 14
  • Thank you @Uriil, for catching and fixing that. :) – Aditya Patil Jun 10 '14 at 17:08
  • @Uriil : Thats awesome mate. Its really helpful. I have accepted you answer. I have tried to add netTcpBinding Binding and mexHttpBinding, and now its throwing error in ECF Test Client. Can you please helpout in this mate. Thank you – Ramu Jun 10 '14 at 21:57
  • @Uriil : I have tried to implement BasicHttp along with netTcpBinding and mexHttpBinding hosted in a Colsole app. However i'm still facing some issue. highlighted under - "Project 2 : BasicHttp and NetTcp Binding hosted in Console App" of my question. I have tried to browse for answer for my best, but no luck. Please can you help out? Thank you!! – Ramu Jun 11 '14 at 11:12
  • You should create new question, post some code. It's hard to help you in comments – Uriil Jun 11 '14 at 11:28
  • @Uriil : I have posted the question [http://stackoverflow.com/questions/24162530/wcf-service-in-console-app-throwing-error](http://stackoverflow.com/questions/24162530/wcf-service-in-console-app-throwing-error). Thank you!! – Ramu Jun 11 '14 at 12:06
0

Sometimes the following web.config line under system.web section could cause problem on your dev machine

<identity impersonate="true" />