I have coded a WCF server side with netTcpBinding. Then I coded a client side. But it shows exception while excute "var sc = new CommondServiceClient();" at runtime. What should I do?
Below is the exception message:
System.InvalidOperationException
HResult=0x80131509
Message=Could not find default endpoint element that references contract 'ICommondService' 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.
Source=System.ServiceModel
StackTrace:
......
I have tried something:
I can consume the services with WcfTestClient.
The service reference is added by visual studio "add service reference...". I guess I get the service mex data. But I meet runtime exception like above
I also tried generate code with svcutil tool, but it still not work
Here is the wcf config file:
<?xml version="1.0" encoding="utf-8" ?>
<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>
<services>
<service name="ServiceContractor.CommondService">
<endpoint address="" binding="netTcpBinding" contract="ServiceContractor.ICommondService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
<!-- 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>
It's a self host wcf service:
var baseAddress = new Uri($"net.tcp://localhost:{PORT}/Company/service");
_host = new ServiceHost(typeof(CommondService), baseAddress);
try
{
var smb = _host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null) _host.Description.Behaviors.Add(new ServiceMetadataBehavior());
_host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
_host.AddServiceEndpoint(typeof(ICommondService), new NetTcpBinding(), "");
_host.Open();
}
catch (CommunicationException ce)
{
_host.Abort();
}
I have no idea what is wrong. What document I should ask for? Can you help me?
But I have another question now. Is it possible to get rid of the configuration. So that the application just needs the DLL and knows nothing about the WCF service.
Generally speaking, there are two ways to call the WCF service (Soap service) in the web application.
Generate the client proxy class by adding service reference, this way
commonly need to configure the service settings in the configuration
file(web.config), most of service information need to be called are
stored in the configuration file. For class library project, we have
to migrate the configuration to the configuration file of the actual
project.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Use the ChannelFactory to create the communication channel, we set
up the service configurations programmatically. From your
description, this way to call the web service might be the
appropriate solution. Customize the service configuration
programmatically in the code. One thing must be noted that all the
service configuration is hard-coded, such as binding type and service
endpoint information. You could refer to the following documents.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.
Related
None of the members of my team has ever been able to get a particular WCF service in our solution to work on our local machines. We've inherited it in legacy code and are trying to replace it, but it's very difficult to tell what it's doing since we can't run the debugger on it and we can't even get a response from it while debugging the main site that uses it.
The main part of our application is a web site. This particular service is hosted in a separate application pool on IIS due to some problems with using Excel interops (which this service uses) in the same app pool as the main site.
The service appears to use net.tcp for the protocol, and I have enabled the Windows Communication Foundation Non-HTTP Activation feature on my machine. I have also enabled the protocol on the Default Website and the node underneath it which is the WCF service in question (is this redundant?).
I can attach the debugger to w3wp.exe processes for both the site and the service. When the site makes the call to the service, however, an error is immediately returned and no breakpoints in the service are hit. The error reads:
The service 'MySvc.svc' cannot be activated due to an exception during
compilation. The exception message is: The type 'MyNamespace.MySvc',
provided as the Service attribute value in the ServiceHost directive,
or provided in the configuration element
Note, I have obviously redacted the real service name, etc, from this post. After attempting to follow solutions proposed on numerous similar questions, I have gotten nowhere. I am wondering if the problem is exacerbated by the separate app pools.
Below is the Web.config from the service project. The SVC file is named UploadAndImport.svc.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
</configSections>
<!-- REDACTED SECTIONS PERTAINING TO LOGGING AND ENTERPRISE LIBRARY -->
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.6.1" />
<identity impersonate="true" />
<pages controlRenderingCompatibilityVersion="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="ProjectName.UploadAndImport">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ExcelServiceEndPointHTTP" contract="ProjectName.IUploadAndImport" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="" name="ExcelServiceEndPointTCP" contract="ProjectName.IUploadAndImport" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- 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>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!-- This line ignores the error that 'An ASP.NET setting has been detected that does not apply in Integratd managed pipeline mode (system.web/identity#impersonate is set to true)'-->
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
</configuration>
The directive in the SVC file looks like this:
<%# ServiceHost Language="VB" Debug="true" Service="ProjectName.UploadAndImport" CodeBehind="UploadAndImport.svc.vb" %>
This is my first WCF service and I am having what seems to be a common problem for newbies i.e. the configuration file.
The service project was created with examples from here and here and after receiving the error [Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.], I went through the MSDN way of Web.config here and here.
Here is my Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Parkalot.WcfClient.Services.UserService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:7349/ServiceMetadata" />
</baseAddresses>
</host>
<endpoint address="/UserService" binding="wsHttpBinding" contract="Parkalot.WcfClient.Services.IUserServiceContract" />
<!-- Adds a WS-MetadataExchange endpoint at "http://localhost:7349/ServiceMetadata/mex" -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metadataSupport">
<!-- Enables the IMetadataExchange endpoint in services that -->
<!-- use "metadataSupport" in their behaviorConfiguration attribute. -->
<!-- In addition, the httpGetEnabled and httpGetUrl attributes publish -->
<!-- Service metadata for retrieval by HTTP/GET at the address -->
<!-- "http://localhost:8080/ServiceMetadata?wsdl" -->
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:7349/ServiceMetadata?wsdl" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The error details are:
Error: Cannot obtain Metadata from http://localhost:7349/Services/UserService.svc
If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.
For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.
WS-Metadata Exchange Error URI: http://localhost:7349/Services/UserService.svc
Metadata contains a reference that cannot be resolved:
'http://localhost:7349/Services/UserService.svc'.
The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.
HTTP GET Error URI: http://localhost:7349/Services/UserService.svc
There was an error downloading 'http://localhost:7349/Services/UserService.svc'.
I have stripped a ton of lines from the error which is probably meant to render in a browser. Since I am running from within Visual Studio, I do not have a browser context.
Ok so here is my scenario.
I have made a WCF Library project. Configured my app.config file. I then created a new console application. Added my WCF Library project to the console app. Then I copied the config file over to my server console app. However, when I run the console application it throws an exception and essentially states that it cant see the app.config file. However, the app.config is clearly inside of the server console application. I can add my endpoints programmatically and make the service work. However, that is not my intention. Is there some sort of trick in order to get the ServiceHost to use the app.config, or more importantly the project to see the app.config?
My app.config
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehavior" name="Services">
<endpoint address="CompanyService" binding="netTcpBinding" bindingConfiguration=""
name="NetTcpBinding" contract="Services.ICompany" />
<endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/Services/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
My Host Service code:
using (ServiceHost host = new ServiceHost(typeof(Company))) {
host.Open();
Console.WriteLine("Press <ENTER> to terminate the host service");
Console.ReadLine();
}
It then throws this exception:
Unhandled Exception: System.InvalidOperationException: Service 'Services.Company' has zero application (non-infrastructure) endpoints. This might
be because no configuration file was found for your application, or because no
service element matching the service name could be found in the configuration fi
le, or because no endpoints were defined in the service element.
What are you naming the app.config file when you copy it over to the console app? There are specific rules that will need to be followed.
If the console app has an executable name of, e.g. "consoleapp.exe", then the app.config will have to have the name "consoleapp.exe.config"
Is the problem that you have called the file app.config, and not yourprogramname.exe.config?
#Jaochim The error says it's looking for a service named Services.Company while your config seems to have it attributed with name="Services" Try changing that.
I have already a few WCF + Windows Service tested and working correctly as run as a Windows Service on remote dev machines. Except for one.
In order to debug, I tried to host the WCF (without the Windows Service) using the built-in "Add Service Reference" and then hosting it in visual studio (don't know what the host is called).
Anyways, I cannot get the Add Service Reference to Discover my services.
Since I am using Windoes Services, I am using TCP stuff. This are some things I have done, all in the winforms app that I am adding the service reference:
Add Project > Properties > Debug > Command line arguments: /client:"WcfTestClient.exe" but the exe doesn't run.
And my App.Config
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:32279/SYNC" binding="netTcpBinding"
bindingConfiguration="tcpSyncBindingConfig" contract="Company.Data.Sync.ILocalCacheSyncContract"
name="tcpSyncClientEP" />
</client>
<bindings>
<netTcpBinding>
<binding name="tcpSyncBindingConfig" maxReceivedMessageSize="6553600" />
</netTcpBinding>
<mexTcpBinding>
<binding name="tcpMexBindingConfig" />
</mexTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="svcBehavior" name="Company.Data.Sync.Services.LocalCacheSyncService">
<endpoint address="net.tcp://localhost:32279/Sync" binding="netTcpBinding"
bindingConfiguration="tcpSyncBindingConfig" name="tcpSyncListenEP"
contract="Company.Data.Sync.Services.ILocalCacheSyncContract" />
<endpoint address="net.tcp://localhost:32279/Sync/mex" binding="mexTcpBinding"
bindingConfiguration="tcpMexBindingConfig" name="tcpMexEP" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="svcBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Side note: the ServiceOperation are returning Microsoft.Synchronization.Data.xxx sutff for example SyncContext. I might have to add a reference in the Winforms? Or will the "Add Service Reference" add them for me? The service and contracts are generated by the Local Database Cache template. perhaps not related to the problem.
After posting, I continued to look for answers and this is what I experimented to be working:
The project file itself e.g. *.csproj for C# project must contain the ProjectTypeGuid for WCF for Visual Studio to even start checking the project for Services.
<ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
More GUIDs can be found at http://www.mztools.com/articles/2008/mz2008017.aspx. (Thanks mztools!)
The WCF project can have multiple services, and each of them needs to to have their config defined in the project's App.Config. Each service needs to have a different mex endpoint address. The services can also share a single servicebehavior which has the serviceMetadata extension defined. Mex endpoint does not need behaviors or binding config. But remember to set mex endpoint contract to IMetadataExhchange.
For TCP mex, the serviceMetadata > HttpGetEnabled must be set to false.
I believe this is the minimum settings.
Right click on the service you wish to host on your PC, then right click and go Debug > Start New Instance
Then go your other application (I assume same solution) and copy the URL it is running at.
Then you can start that one with the same method above and debug both projects on your local machine.
I assume this is what you are trying to do. Please let me know if it was something else.
I have a REST service and I have added it's reference in my WPF application. But as soon as I create a client of my proxy, it throws error and it throws error because my client side app.config is empty:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
This line on client throws error:
HelloWorldClient client = new HelloWorldClient();
This is my system.servicemodel section of web.config on the server side:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="WCFRestExample.HelloWorld">
<endpoint address="" binding="webHttpBinding" contract="WCFRestExample.IHelloWorld"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</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="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Can anybody tell me why is app.config empty? I have also restarted VS2010 but no luck.
NOTE: When I directly browse it in the browser the service is working. So, there is no problem with server side service.
Thanks in advance :)
As some other posts mentioned (such as After creating a wcf service how do I tell whether its restful or soap from the wsdl? and Create a WCF Proxy for a Rest Web Service, among others), Add Service Reference does not work for REST endpoints. You'll need to create the client yourself.
Another issue in your web.config: in order to create a REST endpoint, you need both to have the webHttpBinding (which you do) and add a <webHttp/> endpoint behavior to your endpoint (which you don't).