Configure Service Model for WCF - c#

I have a very basic webservice using WCF (C#, .NET 4.0), to return an hello message.
The deployment under IIS 7 and running it is ok, but when I do svcutil.exe http://localhost:4569/Service.svc?wsdl through the CMD to test the webservice I get:
the remote server returned an error: 415 cannot proccess the message
because the content type 'aplication/soap+xml charset=utf8' was not
the expected type 'text/xml charset=utf8'
When trying to add the service reference (to create a client) I get
An existing connection was forcibly closed by the remote host Metadata
contains a reference that cannot be resolved:
'http://localhost:4569/Service.svc'. Content Type
application/soap+xml; charset=utf-8 was not supported by service
http://localhost:4569/Service.svc. The client and service bindings
may 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'..
I'm pretty sure that the problem is under my Web.config file:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Service">
<endpoint name="soap"
address="http://localhost:4569/Service.svc"
binding="basicHttpBinding"
contract="IService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</services>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Anyway, here's my code:
IService.cs:
[ServiceContract]
public interface IService
{
[OperationContract]
string getMessage();
}
My service.cs has the method
public class Service : IService
{
public string getMessage()
{
return "Ola servico";
}
}
I really don't know what is happening, did some tests after some research but no success.
Service.svc
<%# ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>

You have no service and endpoint defined in your config. Try adding
<services>
<service name="Service"> <!-- name should match the name in your .svc file (if you open it with a text editor) -->
<endpoint name="soap" address="" binding="basicHttpBinding" contract="IService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>

Related

error service metadata is not available while trying to run WCF service

My web.config for WCF service looks like below
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="" name="service1Endpoint"
contract="WcfService1.IService1" />
<endpoint address=""
behaviorConfiguration="WcfService1.AjaxAspNetAjaxBehavior"
binding="" contract="WcfService1.IService1" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="service1Endpoint" />
</basicHttpBinding>
<webHttpBinding>
<binding name="webHttpBinding" />
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2393/Service1.svc"
binding="basicHttpBinding"
bindingConfiguration="service1Endpoint"
contract="ServiceReference1.IService1"
name="service1Endpoint" />
<endpoint address="http://localhost:2393/Service1.svc"
behaviorConfiguration="WcfService1.AjaxAspNetAjaxBehavior"
binding="webHttpBinding" contract="ServiceReference1.IService1"
/>
</client>
<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>
<endpointBehaviors>
<behavior name="WcfService1.AjaxAspNetAjaxBehavior">
<enableWebScript />
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I have another simple aspx page created to test whether service is accessible or not. But when I am trying to run this service error displayed is Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata. Can anyone tell me how to resolve this error.
Thanks in advance.
My web service and contract is as below
namespace WcfService1
{
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
Contract looks like below
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
}
You need a mex service endpoint to allow metadata to be exposed. Add a new endpoint under service node like:
<endpoint address="mex" binding="mexHttpBinding" name="MetadataEndpoint"
contract="IMetadataExchange" />
Read more about Metadata Exchange Endpoint here:
http://www.wcftutorial.net/Metadata-Exchange-Endpoint.aspx

Net TCP binding: The URI prefix is not recognized

This really is bugging me for a couple of hours. I created the simplest WCF service using a TCP binding.
namespace WcfTcpService
{
public class TestTcpService : ITestTcpService
{
public string Hello(string name)
{
return "Hello, " + name + "!";
}
}
}
namespace WcfTcpService
{
[ServiceContract]
public interface ITestTcpService
{
[OperationContract]
string Hello(string name);
}
}
Web.config file has the following section:
<system.serviceModel>
<services>
<service name="WcfTcpService.TestTcpService" behaviorConfiguration="TestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:808/WcfTcpService.TestTcpService" />
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding" contract="WcfTcpService.ITestTcpService"></endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"></endpoint>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="tcpBinding" portSharingEnabled="true">
<security mode="None"></security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<!--<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
<add binding="netTcpBinding" scheme="net.tcp" />
</protocolMapping>-->
<!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->
</system.serviceModel>
This service is hosted in IIS:
Now, when trying to add a reference to net.tcp://localhost:808/WcfTcpService.TestTcpService from a client application, I keep receiving the error:
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost/WcfTcpService.TestTcpService'.
The message could not be dispatched because the service at the endpoint address 'net.tcp://localhost/WcfTcpService.TestTcpService' is unavailable for the protocol of the address.
If the service is defined in the current solution, try building the solution and adding the service reference again.
The net.tcp service is running, I receive the same error with WcfTestClient.exe. and I can succesfully run http://localhost/WcfTcpService/TestTcpService.svc.
I've searched google but nothing came up.
Thanks!
EDIT:
The bindings screen of the 'Default Web site' looks like this btw:
When you create service that uses netTcpBinding and you want to Add service reference in Visual Studio you should use http address (httpGetEnabled) not actual tcp address the service listens on. So the solution was to set localhost/WcfTcpService/TestTcpService.svc as an url in Add service reference dialog.
I had same problem and I changed web.config as below:
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
<baseAddressPrefixFilters>
<add prefix="net.tcp://YourBaseUrl"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>

"Unable to Download Metadata" when using Two Bindings

In my book, it wants me to expose two endpoints using two bindings: WsHttpBinding & NetTCPBinding and host the service in a host application.
I use the following code in C# to try to connect to my service:
Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");
Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
On the service side I have:
[ServiceContract]
public interface IService...
public class SService : IService....
Then in my Config file I have:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WsHttpBindingConfig"
contract="SService.IService" />
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="NetTCPBindingConfig"
contract="SService.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5640/SService.svc" />
<add baseAddress="net.tcp://localhost:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
But when I try to add service reference to my host application, it says unable to download metadata from the address. I don't understand what is wrong and the teacher never taught this.. I decided to go ahead and look it up and learn ahead of time. I used the Editor to create the WebConfig shown above.
Can anyone point me in the right direction? I added Metadata behaviour through the editor and I set the HttpGetEnabled to true.
I can find a few issues with your code that can cause this issue:
Host = new ServiceHost(typeof(SServiceClient), BaseAddress). Pass here typeof(SService.SService) instead of typeof(SServiceClient). Change like this:
Host = new ServiceHost(typeof(SService.SService))
<service behaviorConfiguration="WsHttpBehaviour". I guess this should be "Behavior" as you have defined that. Since you have metadata enabled in config, you may remove the lines of code which add a ServiceMetadataBehavior to your servicehost.
Here is a sample that you can use for reference:
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Service">
<endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
<endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://<machinename>:5640/SService.svc" />
<add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service : IService
{
public string DoWork()
{
return "Hello world";
}
}
}
Metadata will be automatically available at the http baseaddress defined in the config.

Firing Event in WCF Host

I'm running a WCF service hosted in a WPF app, and am trying to raise an event in the host when a message is sent to the service, but am having great difficulty.
My code is as follows:
Service -
namespace BatService
{
[ServiceContract]
public interface IBatServ
{
[OperationContract]
void UseGadget(string name);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class BatServ : IBatServ
{
public void UseGadget(string name)
{
OnUsedGadget(name);
}
public static event EventHandler<BatArgs> UsedGadget;
public static void OnUsedGadget(string name)
{
if (UsedGadget != null)
UsedGadget(null, new BatArgs() { BatGadget = name });
}
}
public class BatArgs : EventArgs
{
public string BatGadget;
}
}
Host -
namespace BatHostWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ServiceHost host = new ServiceHost(typeof(BatServ));
BatServ.UsedGadget += new EventHandler<BatArgs>(BatServ_UsedGadget);
host.Open();
}
void BatServ_UsedGadget(object sender, BatArgs e)
{
MessageBox.Show(e.BatGadget + " was used!");
}
}
}
Service App.config -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="BatService.BatServ">
<endpoint address="" binding="wsHttpBinding" contract="BatService.IBatServ">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/BatService/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<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>
</system.serviceModel>
</configuration>
Host's App.config -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior0" name="BatService.BatServ">
<clear />
<endpoint address="net.pipe://localhost/battserv" binding="netNamedPipeBinding"
bindingConfiguration="" contract="BatService.IBatServ" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/batserv" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
As you can probably guess, I'm expecting to see a MessageBox when I call UseGadget() from a client. Whenever I try to test it out with VS's WcfTestClient.exe, nothing seems to happen at all. Where am I going wrong?
It turns out my endpoints weren't configured correctly. This page helped me solve my problems - http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
I'm not sure if you can "raise events" in wcf, but you can create duplex communication architectures. The netTCPBinding, netNamedPipBinding and wsDualHttpBinding support this functionality.
Here is a video demonstration how to do callbacks with the wsDualHttpBinding
http://www.youtube.com/watch?v=NO2JsLrP75E
I've never done this with netNamedPipeBinding, but i assume the procedure is similar.
Remember to update your app.config files and service references when you're done with your implementation.
Hope that helps!

Service can't find endpoint of other service in WCF

I am trying to create two WCF services which should be able to access each other. However I am getting this error message:
The server encountered an error processing the request. The exception message is 'Could not find default endpoint element that references contract 'AddonWCFService.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.'.
I call the Test() Method from this service
namespace CustomersService
{
[ServiceContract]
public interface ICustomers
{
[OperationContract]
[WebGet]
string Test();
}
public class Customers : ICustomers
{
private int m_i = 0;
public int GetCounter()
{
return m_i;
}
public void Test()
{
AddonWCFService.Service1Client foo = new AddonWCFService.Service1Client();
}
}
}
The other service
namespace AddonWCFWebservice
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void Init();
}
public class Service1 : IService1
{
public void Init()
{
}
}
}
My webconfig:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="MyserviceBehavior" name="CustomersService.Customers">
<endpoint name="ws" address="ws" binding="wsHttpBinding" contract="CustomersService.ICustomers"/>
<endpoint name=""
address=""
binding="webHttpBinding"
contract="CustomersService.ICustomers"
behaviorConfiguration="WebBehavior"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service name="AddonWCFWebservice.Service1" behaviorConfiguration="MyserviceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="AddonWCFWebservice.IService1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyserviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
<customErrors mode="Off"/>
</system.web>
</configuration>
Both services reside in the same active directory of IIS . I added the service reference to the VS C# projects using the web URL i.e. http://www.foobar.baz/Test/Service1.svc and http://www.foobar.baz/Test/Customers.svc
It's probably something obvious but I'm fairly new to the whole WCF business. Thanks!
Update: The solution was to add a client section to my webconfig. Also I used basicHttpBinding over wsHttpBinding because my security will be incorparated elsewhere because it is a public service. I had to match the binding of the client to the binding of the service section: both basicHttpBinding
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
name=""
address="http://demo.mydomain.baz/TestService/Service1.svc"
binding="basicHttpBinding"
contract="AddonWCFService.IService1" />
</client>
<services>
<service behaviorConfiguration="MyserviceBehavior" name="CustomersService.Customers">
<endpoint name="ws" address="ws" binding="wsHttpBinding" contract="CustomersService.ICustomers"/>
<endpoint name=""
address=""
binding="webHttpBinding"
contract="CustomersService.ICustomers"
behaviorConfiguration="WebBehavior"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service name="AddonWCFWebservice.Service1" behaviorConfiguration="MyserviceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="AddonWCFWebservice.IService1"/>
<!--
<endpoint address=""
binding="webHttpBinding"
contract="AddonWCFWebservice.IService1"
behaviorConfiguration="WebBehavior"/>
-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyserviceBehavior">
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
<customErrors mode="Off"/>
</system.web>
</configuration>
The problem with your config is that you have no client configurations. You have only server parts. You need to have client element with endpoints. Take a look here: http://msdn.microsoft.com/en-us/library/ms731745.aspx
If you are not so sure about you config skills I would advise you to open your config with SvcConfigEditor.exe. You will immediately see what's configured.
You can find it here: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\SvcConfigEditor.exe.
If you will do it - you will see that there are no clients configured
I think you specified the wrong service contract in your config file.
This line here:
<endpoint address="" binding="wsHttpBinding" contract="AddonWCFWebservice.IService1"/>
specifies the contract as "AddonWCFWebservice.IService1" when it should be something like "AddonService.IService1" (without the "WCF").

Categories

Resources