WCF: Failed to create a service - c#

I have a WCF service that was unable to make a database connection because I was using plain Sql instead of mySQL. So, I switched over in my code, and now when runnning I get the "Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata" error. Anyone know why this is happening? The client was running fine right before I made the switch, except was not making DB connection.
Here is my web config file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="ConnectString" value="server=xxx;database=xxx;uid=xxx;pwd="xxxx"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

Your metadata can be made reacheable by adding the mex binding for you service.
For https access
<endpoint
address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange"
bindingConfiguration=""
name="MexHttpsBindingEndpoint"/>
For Http access
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
bindingConfiguration=""
name="MexHttpBindingEndpoint"/>

Related

WCF service not running on other devices except server

I have WCF service which runs fine on my server machine using IP, it was working on client side(android studio) but suddenly it stopped working, I tried all solutions from internet but nothing seems to help, Service is sill working on Server browser/Postman.
Here's my web.config file if it is required:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.Service1" behaviorConfiguration="WCFService.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="myWeb" contract="WcfService.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>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="myWeb">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WCFService.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false 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>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<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>
Also my Firewall is disabled completely.
When I ping service from mobile or other pc it gives error:
This site can't be reached.
Err_Address_Unreachable
First, can you call the service successfully from the other computer in the same LAN?
Second, what is the IP of your mobile device?
Finally, have you tried to restart your router/switch device in the current LAN? It is possible that there is something wrong with the router device which provides the Addressing function.
Feel free to let me know if there is anything I can help with.
If your computer's firewall is disabled check if your router has firewall and disable it.

Deploying WCF Service with Database Functionality to IIS

I am net to the use of WCF. I have been struggling with the construct of writing to a database using WCF after deployment. I connect to the WCF via a android Application.
The basic construct that I use is that i implement the WCF with database and attempt to ping the WCF that is deployed on a server by entering the IP and the path.
In my WCF I have two functions. One function Returns the Date and the other function writes to a database using LINQ. When I run the function that must return the date in my browser on android via http://10.0.0.14/jonts/WCFService.svc/date I get a response with no problem. The problem comes up when I run the function to write to the database via http://10.0.0.14/jonts/WCFService.svc/write, I get a 400 Error. But when i run http://localhost:58632/WCFService.svc/write from the host machine it write to the database.
I believe that this is cause by the connection string in my .confic file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<services>
<service name="WCF_Connection.WCFService">
<endpoint address="" behaviorConfiguration="restfulBehavior"
binding="webHttpBinding" bindingConfiguration="" contract="WCF_Connection.IWCFService" />
<host>
<baseAddresses>
<add baseAddress="http://10.0.0.14/bookservice" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<connectionStrings>
<add name="SampleDbEntities" connectionString="metadata=res://*/BooksModel.csdl|res://*/BooksModel.ssdl|res://*/BooksModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\SampleDb.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
<add name="jarvisConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\jarvis.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
The server with IP 10.0.0.14 is running SQL Server Developer 2012.
1)Is it my connection string causing this error?
2)How can I fix this?
1) You're using Integrated Security option in connection strings , so you should check that AppPool in IIS that serves your wcf application is ran under Windows user that has permission to connect your database and perform queries. Or consider to use login/password to specify sql server login (SQL server should be installed with mixed security mode in this case).
2) Use tracing to determing actual WCF error on your server. Or change
<serviceDebug includeExceptionDetailInFaults="false" />
to
<serviceDebug includeExceptionDetailInFaults="true" />
while you are debugging your app.

How to host a WCF service in IIS 7.5?

I am trying to host a WCF service in IIS Service 7.5 but from IIS Manager when I try to browse the svc file it is throwing me this error
Again when I try to run the svc file from the Visual Studio 2012 itself it opens perfectly from me
Where am I doing wrong ?
The web.config file is as follows :
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="GreetMeWcfServiceLibrary.GreetMeService">
<endpoint address="" binding="basicHttpBinding" contract="GreetMeWcfServiceLibrary.IGreetMeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<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>
And I have been trying to define the service first using WCF service library application and then trying to host using WCF service.
Moreover when I right click and click on the WCF configuration of the web.config file it says no service but I have added the reference to that service library dll.
Any help is much appreciated :(
In your WCF project tries to set target FrameWork 4 and perform the publication of the WCF and try again, the configuration file will be different and it should work.
Let me know
In your webConfig.xml try this
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<connectionStrings>
<remove name="XXXXX"/>
<add name="XXXXXX" connectionString="User ID=sa;Password=XXXX;Initial Catalog=XXXX;Data Source=XXXX\SQLEXPRESS"/>
</connectionStrings>
</configuration>
I suggest you review the server configuration to make sure all necessary IIS and WCF components are installed and configured properly. The following articles provide an overview of the issue and related troubleshooting suggestions:
http://www.dotnetscraps.com/dotnetscraps/post/e2809cHTTP-Error-50019-Internal-Server-Errore2809d-in-IIS-7-75.aspx
http://forums.iis.net/t/1166889.aspx

Hosting WCF in console application : Metadata publishing for this service is currently disabled

I have WCF application and I am hosting it locally using a console application. I added the configuration below.
Service class
namespace InboundMessage.Service;
Operator: IOperator {
}
namespace InboundMessage.Service;
IOperator {
}
App.config
<configuration>
<system.web>
<compilation debug="true">
</compilation>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" >
<endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress = "http://X:Port/InboundMessage.Service/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Host Project is a console application that is built and installed in my local machine.
config file has address to the service.
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="ServiceAddress" value="http://X:Port/InboundMessage.Service/"/>
</appSettings>
<system.webServer>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I keep getting Metadata publishing for this service is currently disabled. Am I missing something in my settings. Thank you for your time.
EDIT
It seems to work only if I add the below in the Host configuration but it doesn't seem the right way to host a service.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="meta">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" >
<endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
The configuration (WCF included) has to be in the config file of the assembly that is executed. In your case, it has to be in the config file of the console application.
It is not enough that the WCF is added to the configuration of one of the libraries that the console application uses. This it for two reasons:
Configuration of the libraries is by default not copied into the output folder when you build the main (executable) assembly.
Even if it was copied, it would have a wrong name. WCF configuration is read from the config file that has the same name as the executing assembly.
This is not specific to just WCF, it's the way .NET works.

Communication between two WCF Services in selfhosting Server

In a selfhosting service as described in this MSDN article
there are two servies.
Now I want to call one from the other. One does some database related stuff and the other provides some work. I want to use the database functionality in the other service.
I tried to add a service reference as mentioned here: Stackoverflow with similar question
but I get the message: "There was an error downloading metadata from the address",
so adding a service reference is not possible.
The service on their own both are running and working, as I already use them from client applications.
This is the web.config from the service I want to use.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
And here are parts from the App.config from my selfhosting service
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<!-- omitted lots of blocks -->
<services>
<service name="MyProject.WorkService.GeneralWorkService" behaviorConfiguration="SimpleServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/"/>
</baseAddresses>
</host>
<endpoint address="traceability" binding="basicHttpBinding" name="WorkService" contract="MyProject.Service2.Contracts.IService2"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service name="MyProject.DatabaseService.GeneralDatabaseService" behaviorConfiguration="SimpleServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
<endpoint address="gateway" binding="basicHttpBinding" name="DatabaseService" contract="MyProject.DatabaseService.Contracts.IDatabaseService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<client>
<endpoint name="Service2EP" address="http://localhost/someWork" binding="basicHttpBinding" contract="MyProject.Service2.IService2">
</endpoint>
<endpoint name="DatabaseServiceEP" address="http://localhost/gateway" binding="basicHttpBinding" contract="MyProject.DatabaseService.IDatabaseService">
</endpoint>
</client>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
-update-
I can use a browser window to see my service on
http://localhost:8000
Perhaps there is some other way to use my service. Should I use some proxy
that can be generated with svcutil?
Perhaps there are better ways. Adding the service reference seems not to work
and I cannot tell why.
Are you sure that you have the mex-endpoint configured in the config of the service you are trying to reference?
If you haven't the service will not expose the info required (WSDL) to make the service reference... .
There are at least 4 possibilities:
The metadata exchange mex endpoint is not defined
Metadata exchange is not enabled
You are using the wrong address
You are being blocked by some security setting
From There was an error downloading metadata from the address
Finally found the answer.
Had to copy the endpoints from the selfhosting app.config into the service web.config.
And from that point on, the error messages in the dialog (link "Details" at the bottom)
were helpful.
Just had to add the service behavior to the DatabaseService that I already have defined in the selfhosting app.config.
Thank you all for all your help. Will accept W1ck3dHcH's answer, as it was right, but I just did not see it.

Categories