I was unable to open the connection for my ServicHost. I have initially used the 8080 port it worked before.But now it is not working. I see some error (below) with my App.Conifg.Please let me know where the bug in.
This is a simple EmployeeWCF Demo app with the IEmployeeService interface and EmployeeService class.
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<services>
<service name="EmployeeWCFService.EmployeeService" behaviorConfiguration="mexBehaviour">
<endpoint name ="httpBind" address="EmployeeService" binding="basicHttpBinding" contract="EmployeeWCFService.IEmployeeService" ></endpoint>
<endpoint name ="netTcpBind" address="EmployeeService" binding="netHttpBinding" contract="EmployeeWCFService.IEmployeeService" ></endpoint>
<endpoint name="mexBind" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090"/>
<add baseAddress="net.tcp://localhost:9093"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
ConsoleCode:
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(EmployeeWCFService.EmployeeService)))
{
host.Open();
Console.WriteLine("The connection host is opened at the time " + DateTime.Now);
Console.ReadLine();
}
}
}
Error I am Facing while opening WCF Servicehost:
Additional information: A binding instance has already been associated to listen URI 'http://localhost:9090/EmployeeService'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config.
There may be a typo in your configuration file. The second service endpoint ought to use NetTcpBinding instead of NetHttpBinding, which causes that the two serivce endpoints has the same listening uri address.
Feel free to contact me if the problem still exists.
Related
I just spend 4 hours googling around trying to find why i cant consume (add service reference) WCF hosted in Windows service. Error i am getting is this
The URI prefix is not recognized.
Metadata contains a reference that cannot be resolved: 'net.tcp://localhost:8523/Service1'.
Could not connect to net.tcp://localhost:8523/Service1. The connection attempt lasted for a time span of 00:00:02.0158574. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8523.
No connection could be made because the target machine actively refused it 127.0.0.1:8523
If the service is defined in the current solution, try building the solution and adding the service reference again.
I created a small service following this tutorial https://msdn.microsoft.com/en-us/library/ff649818.aspx to the letter. I'm stuck on step 8 where i get error i posted above.
My App.config (WCF configuration)
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<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>
<bindings />
<services>
<service name="WcfServiceLibrary1.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
name="netTcpEndpoint" contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="" binding="mexTcpBinding" bindingConfiguration=""
name="mexTCPendpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/Service1" />
</baseAddresses>
<timeouts closeTimeout="00:01:00" openTimeout="00:02:00" />
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false"
includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Service.1.cs
public partial class Service1 : ServiceBase
{
internal static ServiceHost MyServiceHost = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (MyServiceHost != null)
{
MyServiceHost.Close();
}
MyServiceHost = new ServiceHost(typeof(Service1));
MyServiceHost.Open();
}
protected override void OnStop()
{
if (MyServiceHost != null)
{
MyServiceHost.Close();
MyServiceHost = null;
}
}
}
Things I've tried so far:
Turning off windows firewall
Opening port TCP 8523 (in and outbound rules)
Changing port numbers
Checking netstat -atn (TCP port 8523 is not listnening)
Double checking that serviceProcessInstaller1 has account option set to Network service
Double checking that serviceInstaller1 has StartType option set to Automatic
Double checking that Service1 is Started in services.msc
Enabled all Net.XXX Services in services.msc
Remove then Added TCP Activation and TCP Port Sharing in "Turn windows features on or off"
Ran out of ideas
Any help as to why i can't consume WCF is highly appreciated.
I have created WCF service to host on IIS.
I am using ServiceHostFactory method to host my service(using Unity as DI).
I want to host my service using multiple binding, over the HTTP as well as over the TCP.
I tried giving base address, but its not taking. still giving me error as Service registered with HTTP schema.
Below code snippet might give you and idea.
public class MyServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType, Uri[] baseAddresses)
{
MyServiceHost serviceHost = new MyServiceHost(serviceType, baseAddresses);
//configure container
MyFactory.Register();
return serviceHost;
}
}
Config file:
<system.serviceModel>
<services>
<service name="My.Service.MyService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000/MyService/"/>
<add baseAddress="net.tcp://localhost:9001/MyService/"/>
</baseAddresses>
</host>
<endpoint address="Question" binding="basicHttpBinding" contract="My.Contract.IQuestionContract" />
<endpoint address="Answer" binding="basicHttpBinding" contract="My.Contract.IAnswerContract" />
<endpoint address="Question" binding="netTcpBinding" contract="My.Contract.IQuestionContract" />
<endpoint address="Answer" binding="netTcpBinding" contract="My.Contract.IAnswerContract" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServicebehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="True">
</serviceHostingEnvironment>
Can anyone suggest me what can be done?
Main thing,
Is it possible to host service using ServiceHostFactory with Multiple Binding?
If Yes, can anyone help me how?
I'd try to remove base net.tcp address and specify full address on endpoint.
I deployed WCF service (.NET 3.5) to IIS 5.1 (WinXP) and it's accessible from local computer. But, it's not accessible outside of local computer.
Here's my web.config file:
<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://222.22.22.222:8072/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior name="Parus.ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://111.1.11.111:8072/AsurReceiveData/Service.svc"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<MyInspector />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="Parus.ServiceBehavior" name="Parus.Service">
<endpoint address="http://222.22.22.222:8072/AsurReceiveData/Service.svc" binding="basicHttpBinding" contract="Parus.IService">
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
<extensions>
<behaviorExtensions>
<add name="MyInspector" type="Parus.MessageInspectorExtension, Parus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
The computer's local IP address is: 111.1.11.111:8072.
The computer's public IP address is: 222.22.22.222:8072.
What I did? I've added new TCP port in IIS 5.1 (Default Web Site Properties -> Web Site tab -> click Advanced button -> click Add button and add new port: 8072).
The project URL looks as following:
http://111.1.11.111:8072/ServiceFolder
The service looks as follows:
public class Service : IService
{
public const string ReplyAction = "http://222.22.22.222:8072/AsurReceiveData/Message_ReplyAction";
public const string RequestAction = "http://222.22.22.222:8072/AsurReceiveData/Message_RequestAction";
public Message SetData(Message requestXml)
{
using (StreamWriter writer = File.CreateText(#"Path\Body.xml"))
{
writer.WriteLine(requestXml.ToString());
}
Message response = Message.CreateMessage(MessageVersion.Default, ReplyAction, requestXml.ToString());
return response;
}
}
I'm not sure if I use local and public IP addresses properly. Maybe it's something else...
I appreciate any help.
You have a firewall on you computer which blocks all incoming calls. You should turn off the firewall or open the 8072 port to be able to communicatie from other computers with you wcf service.
I have a simple WCF Service shown below.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string GetData(int value);
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
The server Web.config file is
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpEndpointBehavior">
<webHttp faultExceptionEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehaviourDebug">
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Diws.Service1" behaviorConfiguration="serviceBehaviourDebug">
<endpoint
address="/basicHttp"
binding="basicHttpBinding"
contract="Diws.IService1"/>
<endpoint
address="/webHttp"
binding="webHttpBinding"
behaviorConfiguration="webHttpEndpointBehavior"
contract="Diws.IService1"/>
<endpoint
address="/wsHttp"
binding="wsHttpBinding"
contract="Diws.IService1"/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
The client is a console app whose App.config is this.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpEndpointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
<wsHttpBinding>
<binding name="WsHttpBinding_IService1" />
</wsHttpBinding>
<webHttpBinding>
<binding name="WebHttpBinding_IService1" />
</webHttpBinding>
</bindings>
<client>
<endpoint
address="http://localhost:50001/Service1.svc/basicHttp"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1"
name="BasicHttpEndpoint_IService1" />
<endpoint
address="http://localhost:50001/Service1.svc/webHttp"
behaviorConfiguration="webHttpEndpointBehavior"
binding="webHttpBinding"
bindingConfiguration="WebHttpBinding_IService1"
contract="ServiceReference1.IService1"
name="WebHttpEndpoint_IService1" />
<endpoint
address="http://localhost:50001/Service1.svc/wsHttp"
binding="wsHttpBinding"
bindingConfiguration="WsHttpBinding_IService1"
contract="ServiceReference1.IService1"
name="WsHttpEndpoint_IService1"/>
</client>
</system.serviceModel>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>
And the client program is this.
class Program
{
static void Main(String[] args)
{
String response = "";
Service1Client basicHttpClient = new Service1Client("BasicHttpEndpoint_IService1");
response = basicHttpClient.GetData(10);
basicHttpClient.Close();
Console.WriteLine(response);
///* Some communication exception
Service1Client webHttpClient = new Service1Client("WebHttpEndpoint_IService1");
response = webHttpClient.GetData(20);
webHttpClient.Close();
Console.WriteLine(response);
//*/
Service1Client wsHttpClient = new Service1Client("WsHttpEndpoint_IService1");
response = wsHttpClient.GetData(30);
wsHttpClient.Close();
Console.WriteLine(response);
Console.WriteLine();
Console.WriteLine("Done");
Console.ReadLine();
}
}
The basicHttpClient and the wsHttpClient work perfectly. However, the webHttpClient throws the exception "System.ServiceModel.CommunicationException was unhandled, HResult=-2146233087, Message=Internal Server Error"
I cannot debug on the servers side as Visual Studio 2012 says
"Unable to automatically debug 'MyProject'. The remote procedure could not be debugged. This usually indicates that debugging has not been enabled on the server."
However, debugging is enabled. I wasn't able to get any insights from using the SvcTraceViewer with diagnostics turned on.
My main interest is figuring out why the REST call using WebHttpBinding is failing, but help getting server side debugging working would be appreciated as well. I'm debugging both the client and the server in VS2012 using multiple startup projects. Localhost is the only server involved.
I understand that the REST endpoint won't show up in WcfTestClient since it provides no metadata exchange, but I expected to be able to call the service through that endpoint and I see no difference between my code and examples of calling RESTful WCF services.
For accessing a REST endpoint try making a HTTP POST request using a browser or HttpClient to the URL : $http://localhost:50001/Service1.svc/webHttp/GetData$. When you use webHttpClient as you do in your code to make a call to the service you are sending a SOAP request which a REST endpoint cannot process. I believe that's the reason your other two endpoints work fine but not this one.
I'm getting this
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
when trying to access the service from my browser. Here is my config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<!-- Note: the service name must match the configuration name for the service implementation. -->
<service name="WcfServiceLibrary.Service1" behaviorConfiguration="MyServiceTypeBehaviors" >
<!-- Add the following endpoint. -->
<!-- Note: your service must have an http base address to add this endpoint. -->
<endpoint contract="WcfServiceLibrary.Service1" binding="basicHttpBinding" address="http://localhost/service1" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="http://localhost/service1/mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost/service1" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
When I type http://localhost/service1 in the web browser I get the 404. But if I remove the app.config below and just simpley do this in the code behind
string serviceUrl = "http://localhost/service1";
Uri uri = new Uri(serviceUrl);
host = new ServiceHost(typeof(Service1), uri);
host.Open();
All works well... Any ideas? Seems simple enough.
I think you are missing the host element under your services:
<service name="WcfServiceLibrary2.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost/service1" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary2.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
Service host does not need URL then.
static void Main(string[] args)
{
var host = new ServiceHost(typeof(Service1));
host.Open();
Console.WriteLine("Host running");
Console.ReadLine();
}
You can show http://localhost/service1?Wsdl in the browser but mex only works with add service reference or WCFTestClient (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE) because you will get the HTTP Bad Request error which comes from the fact that the browser issues an HTTP GET request where the contents of the message are in the HTTP headers, and the body is empty.
This is exactly what the WCF mexHttpBinding is complaining about.