I have two WCF services in two separate DLL files. I want to host those in a single self-hosted project (console host).
I am trying to do this with following code, but I am getting this exception:
The type initializer for
'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.
C# code:
public static void Main(string[] args)
{
new Program().inital();
}
private void inital()
{
ServiceHost myService = new ServiceHost(typeof(ClientNotificationService));
ServiceHost myService2 = new ServiceHost(typeof(ServerNotificationService));
try
{
myService.Open();
myService2.Open();
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadLine();
}
}
In App.config:
<system.serviceModel>
<services>
<service name="ClientNotification.ClientNotificationService">
<endpoint address="net.tcp://localhost:7081/CVClientNotificationService"
binding="netTcpBinding" contract="ClientNotification.IClientNotification">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/ClientNotification/ClientNotificationService/" />
</baseAddresses>
</host>
</service>
</services>
<services>
<service name="ServerNotification.ServerNotificationService">
<endpoint address="net.pipe://localhost/ServerNotificationService" binding="netNamedPipeBinding"
contract="ServerNotification.IServerNotification">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/ServerNotification/ServerNotificationService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
The reason for this error
The type initializer for 'System.ServiceModel.Diagnostics.TraceUtility' threw an exception.
is triggered when the TraceUtility tries to initialize its Event Tracing in an internal method called SetEtwProviderId().
Inspection of the inner exception shows:
Configuration system failed to initialize
and its inner exception shows:
Sections must only appear once per config file.
So it is not your code but your config file that is wrong.
I can see where the confusion on this might start. When you add an Wcf Library project to the solution you'll find this in its app.config:
<!-- 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="WcfServiceLibrary2.Service1">
<!-- rest omitted for brevity -->
So the instruction is to add that config to the app.config of your hosting applicaton, but is isn't very explicit about what exactly to copy.
You need to end-up with an app.config that is valid. You copied the <services> elements to the app.config of the application host. Now you have 2 <services> elements which is an invalid config section. Instead copy the child elements of <services> to the single <services> element in the app.config.
So to be clear:
<!-- 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>
<!-- COPY FROM HERE ... -->
<service name="WcfServiceLibrary2.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary2/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary2.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<!--
.... TO HERE to the app.config of your application hosts config file
-->
</services>
Assuming you already have a basic system.serviceModel config present.
If Visual Studio isn't complaining about your config file you can always start the WCF Service Configuration Editor (under the Tools menu in VS or in the context menu of the config file) to inspect the WCF config. If it is broken it will bark at you.
Related
I try to Host WCF in a Windows Service Using TCP using this article:
http://msdn.microsoft.com/en-us/library/ff649818.aspx
and after all the steps successfully pass include running the service i try to Add a WCF Service Reference to the Client so i created console application project and after try to add reference to my service i got this error:
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.0072566. 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.
This is my 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="WcfServiceLibrary1.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="WcfServiceLibrary1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/Service1" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I also try netstat -ap tcp and cannot find my service connaction
I am getting a "ConfigurationErrorsException" when I try to create a new Instance of the service.
Edit: My App and the WCF service are not on the same machine. The Wcf service is running on a Windows Server 2012.
My Code in the Windows Store app looks like this:
var api = new ServiceRef.MyTestServiceClient(ApiServiceClient.EndpointConfiguration.BasicHttpEndpoint);
My WCF Configuration is (It's hosted as a windows service):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="MyTestService.MyTestService"
behaviorConfiguration="MyTestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding"
name="BasicHttpEndpoint"
bindingConfiguration=""
contract="MyTestService.IMyTestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyTestServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The complete error message is:
The type
'Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior,
Microsoft.VisualStudio.Diagnostics.ServiceModelSink, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' registered for
extension
'Microsoft.VisualStudio.Diagnostics.ServiceModelSink.Behavior' could
not be loaded.
I had the same problem. The following steps have solved the problem for me:
Close Visual Studio
Delete the .suo and .csproj.user files
Restart Visual Studio
Rebuild the project
It is probably because the referenced assembly is not part of the ".NET for Windows Store apps" library. According to this, http://msdn.microsoft.com/en-us/library/windows/apps/br230302(v=vs.110).aspx, diagnostics are not supported:
"Types and members that wrap operating system functionality (such as System.Diagnostics.EventLog and performance counters)."
I have an WCF it is run Successfully when i run it my local host. But i face a problem when run it against a specific domain address(www.esimsol.com). Any one help me how can i config it for ("http://www.esimsol.com/evalservicesite/eval.svc"). My local host config file is :
<?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 behaviorConfiguration="EvalServiceLibrary.Service1Behavior"
name="EvalServiceLibrary.EvalService">
<endpoint address="" binding="wsHttpBinding" contract="EvalServiceLibrary.IEvalService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/EvalServiceLibrary/EvalService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="EvalServiceLibrary.Service1Behavior">
<!-- 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>
Note : Specially base Address
Remove
<identity>
<dns value="localhost" />
</identity>
&
<host>
<baseAddresses>
<add baseAddress="http://localhost:8081/EvalServiceLibrary/EvalService/" />
</baseAddresses>
</host>
you have to publish your solution to your iis server with name http://www.esimsol.com.
after publishing it try running your service with address:http://www.esimsol.com/evalservicesite/eval.svc
and change your base addresss to
you cannot use your loacalhost server 8081 after publishing it to any server either it should be localhost or your website name http://www.esimsol.com
I'm trying to build two webservices with WCF (VS2010).
One webservice runs fine, but when i added a second webservice, then i got the following error:
The contract name 'IMetadataExchange' could not be found in the list
of contracts implemented by the service {0}. Add a
ServiceMetadataBehavior to the configuration file or to the
ServiceHost directly to enable support for this contract.
The second webservice is basically a copy of the first one. So i have no clue why i'm getting this error and how to solve this. Anyone any idea what the problem might be?
Here is my App.config file:
<?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 behaviorConfiguration="WcfEmguCV1.Service1Behavior" name="WcfEmguCV1.EvalService">
<endpoint address="" binding="wsHttpBinding" contract="WcfEmguCV1.IEvalService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfEmguCV1/Service1/" />
</baseAddresses>
</host>
</service>
<service name="WcfEmguCV1.Image">
<endpoint binding="wsHttpBinding" bindingConfiguration="" contract="WcfEmguCV1.IIMage">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfEmguCV1/Service2/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfEmguCV1.Service1Behavior">
<!-- 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>
<behavior name="WcfEmguCV1.Service2Behavior">
<!-- 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>
I found my problem. First of all, i was adding new data to the App.config through "Edit WCF Configuration" option. (Right click on the App.config file and you should see that option).
Since the first webservice config was already by default there, i had to add a new one. So i tried to enter the exact same data. But some things can't be added through that method.
Because when i took a closer look at my XML file i noticed i was missing this for the 2nd webservice:
<service behaviorConfiguration="WcfEmguCV1.Service1Behavior" ...>
So all i had to do was to add that to the 2nd node of 'service'.
behaviorConfiguration="WcfEmguCV1.Service2Behavior"
I have the following configuration file for WCF service. There is a host defined in the config. Still, when I print the service address from the client, it does not know about the host. The printed result is:
http://localhost:3187/Service1.svc/MyFolder
Why doesn’t it take into account the host name? What modification do we need to do for it?
Note: I am running from VS 2010 for running service and client website.
Service1Client myClientService = new Service1Client();
Response.Write(myClientService.Endpoint.Address);
Client Configuration (Autogenerated by Visual Studio)
<client>
<endpoint address="http://localhost:3187/Service1.svc/MyFolder"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
contract="MyWCFReference.IService1" name="WSHttpBinding_IService1">
<identity>
<userPrincipalName value="U16990#ustr.com" />
</identity>
</endpoint>
</client>
The server side configuration is:
<services>
<!--MyService-->
<service name="MyWCFServiceApplication.MyService"
behaviorConfiguration="WeatherServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:80/ServiceModelSamples/FreeServiceWorld"/>
</baseAddresses>
</host>
<endpoint address="MyFolder"
binding="wsHttpBinding"
contract="MyWCFServiceApplication.IService1" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WeatherServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
When a WCF service is hosted in an ASP.NET process, through either IIS or the ASP.NET Development Server (a.k.a Cassini), the baseAddresses setting in the service's configuration file is ignored since the service will always be reachable through the URL of the SVC file.
The URL you're seeing on the client is therefore correct:
http://localhost:3187/Service1.svc/MyFolder
As you can see, the base address of the service becomes the URL of the SVC file on the web server.
You're talking about a WCF client - yet, the config you posted only contains config for a service (the server side) ... (the <services> section).
I can't see any client configuration in what you posted - there ought to be a <client> section in your config somewhere