How to add service reference for a netTcpBinding service? - c#

Simple problem :
How to self host a netTcpBinding service in client project right clicking Service References > Add Service Reference ...
Clicking "Discover" it references all services using WsHttpBinding including the netTcpBinding one. But when expanding the netTcpBinding one it can't navigate to the contract and it throw the following error :
Could not find a base address that matches scheme net.tcp for the
endpoint with binding MetadataExchangeTcpBinding. Registered base
address schemes are [http].
I found the following .Net WCF sample very nice and neat, but in the example, it doesn't show how the config file is set, and how we could get the proxy via the wizzard.
C:\WF_WCF_Samples\WCF\Basic\Binding\Net\Tcp\Default\CS
Here is the service config file ... (I don't use any base adress, it should work without it I think)
<service behaviorConfiguration="Canopus.WebServices.LogAndNotificationService"
name="Canopus.WebServices.LogAndNotificationService">
<endpoint address="" binding="netTcpBinding"
contract="Canopus.WebServices.ILogAndNotificationService" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
<behaviors>
<serviceBehaviors>
<behavior name="Canopus.WebServices.LogAndNotificationService">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
Thanks for your help !

I think your service behaviour cannot have httpGetEnabled attribute when u r dealing with netTcPBinding. and also looks like ur end point address contains something like http:// it should be net.tcp://.

Related

exposing net.tcp endpoint

I'm a bit confused of how to expose an endpoint in WCF
I've got a tcp endpoint and a mex tcp endpoint.
<service name="MessageReaderService.MessageReaderService">
<endpoint name="NetTcpReaderService"
address="ReaderService"
binding="netTcpBinding" bindingConfiguration=""
contract="Contracts.IMessageReaderService" />
<endpoint name="netTcpMex"
address="mex"
binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8082" />
</baseAddresses>
</host>
</service>
When I try to run this in the service host I get the following exception :
The contract name 'IMetadataExchange' could not be found in the list of contracts
implemented by the service MessageReaderService. Add a ServiceMetadataBehavior to the
configuration file or to the ServiceHost directly to enable support for this contract.
So I conclude from this error that I need to add a service behavior to expose metadata.
So I added the behavior :
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
but then I get a different error :
The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the
HttpGetUrl property is a relative address, but there is no http base address. Either
supply an http base address or set HttpGetUrl to an absolute address.
So now I have to actually add another endpoint (http) to expose the metadata over mexhttpbinding ?
is there a simple way to expose the endpoint over tcp ?
Two things:
(1) once you've defined the service behavior, you of course must also apply it to the service!
<service name="MessageReaderService.MessageReaderService"
behaviorConfiguration="ServiceBehavior">
(2) you don't need an HTTP endpoint - you don't need to have an HTTP URL - just define this service behavior like this:
<behavior name="ServiceBehavior">
<serviceMetadata />
</behavior>
Your metadata is now available over a mexTcpBinding endpoint - you cannot browse to it using HTTP, but a client can definitely connect to it and use it!
You can verify this by using the WCF Test Client and going to either
net.tcp://localhost:8082 (the base address)
or
net.tcp://localhost:8082/mex (the mex address)
in both cases, the WCF Test Client should now find your service and be able to discover its capabilities.

Why do I have to specify my address in my WCF configuration (it is actually working)

I have set up a wcf service and when I browse to the .svc file I am presented with a screen that says...
You have created a service.
So I know that the service is running successfully. However I have looked in the configuration and it does not actually have an address.
<system.serviceModel>
<services>
<service behaviorConfiguration="CountryProvinceBehavior" name="CountryProvinceWCFService">
<endpoint address="" binding="webHttpBinding" contract="ICountryProvinceWCFService" behaviorConfiguration="CountryProvinceBehavior"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CountryProvinceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="CountryProvinceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
I then wrote a console app to test the service.
I got thr url from the browser and added a 'Service Reference' from a console application I was expecting the project to create enpoint defintions in my configuration file. And then when I create a client I could load up one of these configs but they aren't there....
You don't have to specify the address in configuration, although in this case you do specify the address for the endpoint (""). That means that WCF will use it as an address relative to the service base address. If the service is hosted in IIS, then it'll be the address of the .svc file. If the service is self-hosted (i.e., using ServiceHost directly), then the address will be the one passed to the ServiceHost constructor.

I've created a WCF service and now I want to call it from a reference but why InvalidOperationException?

I've written a WCF service. I have successfully browsed to the service and it says:
You have created a service.
So then I add a reference to it using a the 'Add Service Reference' in visual studio. Then I write the following code to consume it....
ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServiceClient("VLSContentServiceEndpointBehaviour");
List<ServiceReference1.Category> cats = client.GetCategoriesByGET();
But im getting the error:
Could not find endpoint element with
name
'VLSContentServiceEndpointBehaviour'
and contract
'ServiceReference1.IVLSContentService'
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 name
could be found in the client element.
It makes no sense because the argument 'endPointConfigurationName' matches what I have set it to in the service. Here is the service configuration:
<system.serviceModel>
<services>
<service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
<endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="VLSContentServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="VLSContentServiceEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Whats going on?
You are using REST service - client for such service cannot be created with Add service reference. That is only for SOAP services (no webHttpBinding and webHttp behavior). Also once you use SOAP service you don't pass name of any server side feature to the constructor of the proxy. The proxy constructor expects name of client endpoint from client's configuration.
How to consume REST service
Looks like your contract is not correct. you have:
contract="IVLSContentService"/>
and it is expecting:
contract="ServiceReference1.IVLSContentService"/>
according to the error message.
also your endpoint address is empty. Does that not need to contain something?
The VLSContentServiceEndpointBehaviour parameter while creating an instance of client is the Name of the endpoint and not the endpoing behaviour.
Change
<endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
to
<endpoint address="" name ="Client" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
and create a service client as
ServiceReference1.VLSContentServiceClient client = new ServiceReference1.VLSContentServiceClient("Client");
Also your address is missing which is bit strange.

WSDL file is missing for WCF WebService

I created simple wcf service with default functionality and hosted in IIS7. Its working fine and rendering data to the client. But when I try to click on wsdl link in the service its showing "Page cannot be displayed".Let me know what will be the problem
.
When I try to click the link below (http://win-nsms.smsserver.com/VirtualFolder/MyService.svc?wsdl), WSDL file is not getting displayed in the browser. Instead I am getting "The page cannot be displayed" error in the page
Now If I change the "win-nsms.smsserver.com" to "localhost" in the URL, WSDL file is getting displayed.
Yes I added behaviour configuration in my config as follows
<system.serviceModel>
<services>
<service name="WcfServiceSample.Service1" behaviorConfiguration="WcfServiceSample.Service1Behavior">
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceSample.IService1">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://win-nsms.smsserver.com"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceSample.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
Have you allowed retrieval of service meta data?
In the behaviours section of your config file, add a new behaviour like this:
<behaviors>
<serviceBehaviors>
<behavior name="HttpGetMetadata">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
Then tell your service to use this behaviour:
<system.serviceModel>
<services>
<service name="MyService"
behaviorConfiguration="HttpGetMetadata">
....
This is telling your service to allow the service metadata (the WSDL) to be retrieved via http. To confirm you can browse to the appropriate URL.
Is "mymachinename.domainname.com" added as a hostmask in IIS for the site?
You may need to setup the base address for the service.
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://mymachinename.domainname.com"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
I had a similar problem where the service was working 100% for domain.com but not www.domain.com. I had to setup the latter as a redirect to the former and set the base address to the former.
HTH!
Did you enable WDS exposure? Standard settings dont show the WDSL.

Unable to properly reference a wcf dataservice

I'm having a couple of issues which may be related, or may not. I noticed that when I use Add Service Reference in Visual Studio to add a reference to my data service, the reference.cs it generates does not compile. It complains about a missing namespace. I can fix it up to compile, but this happens every time I update the reference, and it's worrying on other levels too, such as "will this cause other issues".
I also noticed that when I do this, my host server (a console application hosting the data service) logs this:
An exception occurred [System.Data.Services.DataServiceException] :: The URL
representing the root of the service only supports GET requests.
This is the service config:
<service behaviorConfiguration="behaviour" name="StatsPlus.Server.HostedServices.SPDataServiceHost">
<endpoint address="svc" binding="webHttpBinding" contract="System.Data.Services.IRequestHandler" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8752/DataService/"/>
</baseAddresses>
</host>
</service>
And the behaviour:
<behavior name="behaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="10"/>
</behavior>
When I try to run svcutil http://localhost:8752/DataService/, I get this:
HTTP GET Error
URI: http://localhost:8752/DataService
There was an error downloading 'http://localhost:8752/DataService'.
The request failed with HTTP status 405: Method Not Allowed.
Any ideas? Much appreciated
Thanks
I think you're connecting to a wrong address. You have a base address
<add baseAddress="http://localhost:8752/DataService/"/>
and on top of that a relative endpoint address
<endpoint address="svc" binding="webHttpBinding"
so your complete URL will be the combination of the two:
http://localhost:8752/DataService/svc
Did you try to connect there??
I am not sure if you can have a "mex" metadata exchange endpoint with WCF REST services, really. I was under the impression that the client-side proxy for a WCF Data Service gets its metadata over a special URL call from the HTTP endpoint. So maybe try to remove that from your config as well (and you can't use svcutil on that service, I believe - svcutil is only for SOAP service calls, if I'm not mistaken).
Also, since you're using webHttpBinding and self-hosting, you need to add the webHttp behavior:
<behavior name="behaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<dataContractSerializer maxItemsInObjectGraph="10"/>
<webHttp />
</behavior>
If you do these two steps, I think you should be able to get at your WCF Data Service. Try it and let us know!

Categories

Resources