Deploying WCF Service with Database Functionality to IIS - c#

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.

Related

WCF: HTTP 404.17 NOT FOUND after depyloing to local iis

I created a restful web service in WCF. Everything works perfectly fine if i host it via IISExpress in Visual Studio. But after deploying it to local IIS, a 404.17 NOT FOUND error occures, when trying to access the .svc service.
It states:
"Not Found The requested content appears to be script and will not be served by the static file handler"
I already tried these solutions:
Launch Command Prompt - Start - cmd.exe cd C:\Windows\Microsoft.NET\Framework64[Dot Net Version] aspnet_regiis -ir
and
"C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation" and execute"servicemodelreg -i" to install them manually.
which did not change anything.
I'm pretty sure it's a configuration problem but can't figure it out since I'm quite unexperienced in doing this.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.6.2"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpServiceBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyService.Service" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost/"/>
</baseAddresses>
</host>
<endpoint binding="webHttpBinding" contract="MyService.IMyService" behaviorConfiguration="webHttpServiceBehavior"/>
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\myDB;initial catalog=DB;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
</configuration>
I'm pretty frustrated after trying for days because developing this service was real fun until deployment failed completely.
I would really appreciate any hints.
Thanks!
Solved it:
The problem was that there was no IIS APPPOOL Login for my SQLSERVER Database configured.
Here is the step by step explanation: https://stackoverflow.com/a/7698316/7547137

Hosting Web Service in IIS - Not able to access the service

I have a WCF Service Application project in Visual Studio that contains the following files
ZooWebService.svc
WebConfig
I attempted to host the Web Service in IIS but when i browse to my web service from IIS the Web Service does not load at all. It just says "loading" in the browser.
Here is my WebConfig file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="Default"
name="WCFWebService.ZooWCFService">
<endpoint address="" binding="basicHttpBinding"
contract="WCFWebService.IZooWCFService" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I did the following so what have i done wrong?
Open IIS
Right clicked on Default Website
Selected "Add Application"
Populated "Alias" field
Populated "Physical Path" to directory of where the solution contents are
Clicked OK
Rebuilt the solution in Visual Studio
We don’t need to specify the base address in the configuration file which needs to be set up in the web site binding module.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d42fc165-052d-476a-9580-1240b3d0293d/specify-endpoint-in-wcf-webconfig?forum=wcf
then we should enable the WCF feature in the IIS.
At last we could see the web service description in http://ip:port/xxxx.svc.
Feel free to let me know if there is anything I can help with.
For enabling the WCF in IIS, you may have to enable the HTTP activation feature for the .net framework first.
Some of links to do this
https://docs.sdl.com/LiveContent/content/en-US/SDL%20Tridion%20full%20documentation-v1/GUID-326D6F7B-08EC-43EB-A5A7-9C51DD9E555C
https://pubs.vmware.com/mirage-56/index.jsp?topic=%2Fcom.vmware.mirage.api.pg.doc%2FGUID-552D845B-E530-4898-A06B-4F73E668BEFF.html
Once this is done you may try to deploy WCF directly from the visual studio to your local IIS>
Right click on the WCFproject -> go to properties -> in properties window -> select web tab -> in servers section select Local IIS and click on Create Virtual Directory.
Now run the WCF from Visual studio, this will host the WCF in local IIS.

WCF Service runs perfectly when running under iis express but same service wont work when running under Local IIS with its virtual directory

I was practising WCF and stuck to this situation.
My Wcf service works perfectly under IISExpress (default for VS2013).
But When I use the same under Local IIS, I got below exception in WCF Test Client.
Error: Cannot obtain Metadata from
http://localhost/WcfService1/Service1.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/WcfService1/Service1.svc Metadata contains
a reference that cannot be resolved:
'http://localhost/WcfService1/Service1.svc'. The remote server
returned an unexpected response: (405) Method Not Allowed. The remote
server returned an error: (405) Method Not Allowed.HTTP GET Error URI:
http://localhost/WcfService1/Service1.svc There was an error
downloading 'http://localhost/WcfService1/Service1.svc'. The request
failed with HTTP status 404: Not Found.
My web config has endpoint for metadata exchange and also behaviour has httpGetEnabled true.
See web.config:
<?xml version="1.0"?>
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="f:\tfs\wcfservice1\wcfservice1\web_messages.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="true"
logMessagesAtTransportLevel="true" />
</diagnostics>
<services>
<service behaviorConfiguration="behave" name="WcfService1.Service1">
<endpoint address="WcfService1.Service1" binding="basicHttpBinding" name="basicHttp"
contract="WcfService1.IService1" />
<endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/WcfService1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behave">
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</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>
I checked other WCF questions. but couldn't find answer.
Thanks in advance.
When I use Local IIS, I am able to hit url
http://localhost/wcfservice1/
and it shows me all files at that directory.
but when I click on Service1.svc, it shows IIS error page.
HTTP Error 404.3 - Not Found The page you are requesting cannot be
served because of the extension configuration. If the page is a
script, add a handler. If the file should be downloaded, add a MIME
map.
After addng WCF activation in IIS. Now I am getting different Error.
I couldn't understand it.
You need to enable WCF in IIS Manager for your site, see: https://blogs.msdn.microsoft.com/blambert/2009/02/13/how-to-enable-iis7asp-net-and-wcf-http-activation/
Once WCF is enabled for IIS you should be able to run everything without a problem.
I resolved issue.
I have turned on .Net framework 4.6 advanced services > WCF services > HTTP activation.
Now WCF service is working perfectly.
Thanx to gmiley.. your previous answer led me there.

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

WCF: Failed to create a service

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"/>

Categories