Getting hosted IP address of WCF service - c#

I have wcf service which running with http and net.tcp protocol on IIS. Now I want to get live IP address with port that is assigned to them respectively. Furthermore,I want it in wcf service project. Please give your suggestion.

When you set up your WCF service using a web.config file, you specify the port as shown in the web.config below in the baseAddresses element:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ServiceClassName">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:12345/ServiceClassName"/>
</baseAddresses>
</host>
<endpoint address=""
binding="netTcpBinding"
contract="ServiceInterfaceName" />
</service>
</services>
</system.serviceModel>
</configuration>
The IP address is a bit more tricky. Is there a reason that the IP address is not known at runtime or is there a specific reason that you cannot use the host name to connect to your service?

Related

WCF Web Service not deploying on localhost ... web.config ... endpoints

Trying to deploy a super barebones WCF Web Service and test it locally. I'm getting an error:
Could not find a base address that matches scheme https for the endpoint
with binding BasicHttpBinding. Registered base address schemes are [http].
I'll include my endpoint tags from the web.config file here:
<services>
<service name="Company.PrototypeWebService.PrototypeWebService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureHTTPBinding" contract="Company.PrototypeWebService.ServiceContracts.IPrototypeWebService" />
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
Is there something I'm missing here? I'm new to web service development.
This is because your base address is Http, but I see that your metadata endpoint uses https, which is the cause of the error,modify your Service:
<services>
<service name="Company.PrototypeWebService.PrototypeWebService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="secureHTTPBinding" contract="Company.PrototypeWebService.ServiceContracts.IPrototypeWebService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
Modify mexHttpsBinding to mexHttpBinding.
If you want to use https binding, you need to configure the SSL certificate on the server. Normally, it is more convenient to use https in IIS.
UPDATE
If hosted in IIS, we need to create the following WCF project:
The directory of this project looks like this:
We can deploy the project directly to IIS, set the base address when deploying in IIS, we do not need to configure the base address in the configuration file. This is the base address of the service:
After the deployment is successful, we click on the .svc file to view the service:

How to resolve AddressAccessDeniedException in WCFhost

I created a simple WCF service and hosted in a console application.
After configuring all the endpoints I got an exception as below.
Below in my configuration file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" contract="HelloService.IHelloService" binding="basicHttpBinding"></endpoint>
<endpoint address="HelloService" contract="HelloService.IHelloService" binding="netTcpBinding"></endpoint>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/"/>
<add baseAddress="net.tcp://localhost:8090/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Help me how to resolve this error?
The error occurs because of permission problem in your operating system.
Your operating system does not allow WCF service on a specified port.
All you need to do is to create an exception for that port.
Below is the procedure:
Click Start
Run cmd prompt as an administrator
type netsh.exe and press enter
type http add urlacl url=http://+:8081/Service/ user=administrator and press enter.
Here you need to observe three points
8081: is your port on which you need access. Replace it with your own port number.
Service: this is your sesrvice name. Ex: MyWeatherService
administrator: you have to give access to the ser you want on your computer. In my case I gave access to adminitrator. Ex: you can give access to the puppy if there is an user named puppy on your machine.
Check first if the WCF service can user port 80
run your application as administrator, it will resolve the issue.

Self hosted wcf service on azure vm windows server

I have wcf service with udp binding (new in WCF 4.5) and I'm trying to host it on Windows Server 2012 on Azure.
I did endpoint mapping on Azure for port I need (39901; it works for HTTP:80, I can see IIS website) and I allowed all traffic in firewall for this port.
But I still can't get wsdl in web browser.
Here is app.config for console app:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="12" maxConcurrentInstances="56" maxConcurrentSessions="12" />
<useRequestHeadersForMetadataAddress></useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ServerService.UdpServiceAlpha">
<endpoint address="soap.udp://localhost:8091" binding="udpBinding" contract="ServerService.IUdpServiceAlpha" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:39901/SelfServerHost/" />
</baseAddresses>
</host>
</service>
</services>
<protocolMapping>
<add binding="udpBinding" scheme="soap.udp" />
</protocolMapping>
<bindings>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
For localhost everything works fine... I'm new in this (azure, windows server deployment) and I tried a lot of ideas here on stackoverflow or anywhere else. But still not working.
Any idea?
EDIT1:
<services>
<service name="ServerService.UdpServiceAlpha" behaviorConfiguration="UdpServiceAlpha.Behavior">
<endpoint address="/" binding="udpBinding" contract="ServerService.IUdpServiceAlpha"/>
<endpoint address="/" binding="webHttpBinding" contract="ServerService.IUdpServiceAlpha"/>
<host>
<baseAddresses>
<add baseAddress="http://xxx.cloudapp.net:39901/SelfServerHost/" />
<add baseAddress="soap.udp://xxx.cloudapp.net:39901/SelfServerHost/" />
</baseAddresses>
</host>
</service>
</services>
Try changing the Azure VM Endpoint public port from 39901 to 443!
A hell lot of sys-admins and ISPs block all outgoing ports, and only allow few (outgoing port white listing). Port 80 and 443 are usually never blocked because it is usually HTTP traffic.
Change only the public port for the VM Endpoint. Leave the private port as is - 39901.
Your configuration file seems to have a few issues:
The service behavior is missing a name:
<serviceBehaviors>
<behavior name="my.service.behavior"> ...
The service needs to reflect the binding configuration
<service name="ServerService.UdpServiceAlpha" behaviorConfiguration=" my.service.behavior ">
Yeah, finally... got the solution for this. My friend (network big boss :) ) told me how port communication works... Idea was simple: "check once again server and client firewall ports", and here is that point "client firewall".
I allowed only server firewall ports, but there should be allowed communication for client. Added TCP/UDP Outbound rule in client PC and it works magically.
... and for UDP I needed to change TTL (default is 1).

WPF Client with WCF Service from Localhost dev to prod server?

I am just starting to learn WCF. I have successfully set up a WCF service with host console app. The service maps to SQL Server with Entity Framework. I then have a WPF client app which consumes the service. This all works fine in Dev, both the service and client app running from my machine:
<endpoint address="http://localhost:8081/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IRegimesService" contract="RegimesService.IRegimesService"
name="BasicHttpBinding_IRegimesService" />
I have now transferred the service and host onto my new VM. The service host runs fine. I am now trying to configure the client app endpoint to connect to the service. I think the HTTP address is incorrect:
Service app.config:
<services>
<service name="diiRegimesService.RegimesService">
<endpoint address="" binding="basicHttpBinding" contract="diiRegimesService.IRegimesService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8082" />
</baseAddresses>
</host>
</service>
</services>
Client app.config:
<client>
<endpoint address="http://emea-myserver01.emea.basketballinfo.com/localhost:8082/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IRegimesService" contract="RegimesService.IRegimesService"
name="BasicHttpBinding_IRegimesService" />
</client>
The server credentials for remote desktop are: emea-myserver01.emea.basketballinfo.com
I currently don't specify a username & password in my client app.config.
The service is running on the server. I'm getting an exception trying to add the service reference in the client app.
Add Service Reference- Adress: http://EMEA-myserver01.emea.basketballinfo.com
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://emea-myserver01.emea.basketballinfo.com/'.
You need to make sure the server is able to resolve the full domain name.
Also, the address url provided in the client configuration snippet <endpoint address="http://emea-myserver01.emea.basketballinfo.com/localhost:8082/ does not seem valid. Perhaps you want to use <endpoint address="http://emea-myserver01.emea.basketballinfo.com:8082/

Failed to invoke the service. Possible causes: The service is offline or inaccessible while Invoking

I have my service configured as below:
<system.serviceModel>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="net.tcp://127.0.0.1:808/service" />
</baseAddresses>
</host>
<endpoint address="net.tcp://127.0.0.1:808/service/"
binding="netTcpBinding"
contract="WcfService1.IService1"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<protocolMapping>
<add scheme="net.tcp" binding="netTcpBinding"/>
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="MyEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
and the client as:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService1" sendTimeout="00:05:00" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://127.0.0.1/service/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService1" contract="IService1"
name="NetTcpBinding_IService1">
<identity>
<servicePrincipalName value="host/MachineName" />
</identity>
</endpoint>
</client>
</system.serviceModel>
When using WCFTestClient or SVCutil, I am able to discover and access the servie and generate proxy or stubs.
But when I want to invoke any of the methods getting following error:
Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:04:59.9980468'.
Set the following value inside the endpoint block
<identity>
<dns value ="localhost"/>
</identity>
Maybe not the answer you are looking for, but for development on the local machine I always use an HTTP endpoint. I find Net.TCP will only play nice when it is hosted on a server in IIS.
It is easy enough to add the NET.TCP endpoints once you deploy to an IIS server. Note: that the base address no longer has any affect as that is set in IIS. To test the new endpoints it is best to start by opening the service in a browser while remoted into the server, that way you get the best error messages.
If i understand correctly you are trying to select the endpoint when creating the service reference? Thats now how it works.
If you create your service reference using 127.0.0.1/service/Service1.svc
You should see in your config file something like the following. (When I create my service endpoints I always name them with the protocol as a suffix.
<endpoint address="http://servername:8090/servername/servername.svc/Business"
binding="basicHttpBinding" bindingConfiguration="BusinessHTTP"
contract="servername.IService" name="BusinessHTTP" />
<endpoint address="net.tcp://servername:8030/Service/Service.svc/Business"
binding="netTcpBinding" bindingConfiguration="BusinessTCP"
contract="servername.IService" name="BusinessTCP" />
then in your code you can choose which endpoint to use.
Dim svc as New SeviceReferenceName.BusinessClient("BusinessTCP")"
I have faced the same issue. It was the issue of port address of EndpointAddress. In Visual studio port address of your file (e.g. Service1.svc) and port address of your wcf project must be the same which you gives into EndpointAddress. Let me describe you this solution in detail which worked for me.
There are two steps to check the port addresses.
In your WCF Project right click to your Service file (e.g. Service1.svc) -> than select View in browser now in your browser you have url like http://localhost:61122/Service1.svc so now note down your port address as a 61122
Right click your wcf project -> than select Properties -> go to the Web Tab -> Now in Servers section -> select Use Visual Studio Development Server -> select Specific Port and give the port address which we have earlier find from our Service1.svc service. That is (61122).
Earlier, I had a different port address. After specifying the port address properly, which I got from the first step, EndpointAddress 61122, my problem was solved.
I hope this might be solved your issue.
NetTcp binding is secured by default.In the security authentication process between client and service,WCF ensures that the identity of service matches the values of this element. Therefore service need to be configured with :
<identity>
<dns value ="localhost"/>
</identity>

Categories