WCF with SQL table - exposing metadata - sql

I developed small WCF service which takes all records from table. It looks like that:
IService1.cs:
[ServiceContract]
public interface IService1
{
List<badanieCis> GetRecords();
}
Service1.svc.cs:
public List<badanieCis> GetRecords()
{
przychodniaEntities dataContext = new przychodniaEntities();
return dataContext.badanieCis.ToList();
}
And I am getting a message: Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata
What I have made after some research was changing markup in svc file for this one:
<%# ServiceHost Language="C#" Debug="true"
Service="Harvesting.Service.HarvestingService" CodeBehind="Service1.svc.cs" %>
But still nothing.
My web.config 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>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<connectionStrings>
<add name="przychodniaEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=DAREK-PC\SQLExpress;initial catalog=przychodnia;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>

You are missing service definition in the config file. You need something like:
<services>
<service behaviorConfiguration="..." name="Service1 with namespace">
<endpoint address="..." name="..." binding="wsHttpBinding" bindingConfiguration="...." contract="IService1 with unterface" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<services>
...
You may use this tool to help with config.

Related

Rest WCF that accesses a SQL Server database

I have a RESTful WCF service that accesses a SQL Server database. Using it I can access the database when it runs on IIS Express, but when it runs on local IIS, it returns nothing from the database!
Here is the the method in the contracts:
[OperationContract]
[WebGet(UriTemplate = "/getdata",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<DbData> getData();
and here is the web.config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="AlamalConStr"
connectionString="Data Source=.; Initial Catalog=trial_web_service_db;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Windows" />
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<services>
<service name="AlamalBankWCFService.AlamalService">
<endpoint kind="webHttpEndpoint" contract="ServiceLib.IAlamalService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="wsHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
When I run this WCF service on IIS Express, it returns the data from the database, but when I host it on Local IIS and run it it does not return data from database it shows empty page with [] only!
You might have to check the userid of your app pool. Check with that app pool user id has access to connect to SQL server. Either add the app pool id to sql server or change connection string to use SQL authentication. i.e. Use user id and password in connection string for a user created in sql server.
Some more help here.
http://msdn.microsoft.com/en-us/library/ff648340.aspx

Bad request 400 error while acessing wcf rest service frequently

I am facing the problem of Bad request 400 while accessing the wcf service. I have tried all the solution related to this topic but still not solved. Wcf service is on IIS7 .
I am trying to call the service with below code.
try
{
WebClient client = new WebClient();
byte[] data = client.DownloadData(ApplicationRunTimeSettings.ServiceURL() + userID);
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
result = obj.ReadObject(stream).ToString();
}
catch (Exception)
{
}
return result;
The config file at service is below, the config file is same for the wcf as well as web application. Actually wcf service is developed with in the web application and the web app hosted on iis7 and we are accessing the service with in it.
The configuration file is below. Most of the time it does not return error but it is breaking after some time. Request on the wcf service is frequent . Data is form of JSON.
Now after making the below suggested changes for serviceThrottling the web.config file look like mentioned below but it still gives the same error some times.
<system.web>
<sessionState timeout="1440"/>
<customErrors mode="Off"/>
<httpRuntime executionTimeout="90" maxRequestLength="104857600" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true"/>
<!--set compilation defug="false" when releasing-->
<compilation targetFramework="4.0" >
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="86400"/>
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- maxAllowedContentLength = bytes -->
<requestLimits maxAllowedContentLength="104857600"/>
</requestFiltering>
</security>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.serviceModel>
<services>
<service name="Glance.DynamicBusinessService.DynamicBusinessService" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="Glance.DynamicBusinessService.IDynamicBusinessService"/>
<endpoint address="" binding="webHttpBinding" contract="Glance.DynamicBusinessService.IDynamicBusinessService" behaviorConfiguration="REST">
<!--
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.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="throttleThis">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True" />
<serviceThrottling
maxConcurrentCalls="40"
maxConcurrentInstances="20"
maxConcurrentSessions="20"/>
<!-- 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>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="999999999" receiveTimeout="24" closeTimeout="24" maxBufferPoolSize="999999999" maxBufferSize="999999999">
<readerQuotas maxDepth="32" maxStringContentLength="999999999" maxArrayLength="99999" maxBytesPerRead="4096" maxNameTableCharCount="99999" />
</binding>
</webHttpBinding>
<customBinding>
<binding name="basicConfig">
<binaryMessageEncoding/>
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
</system.serviceModel>
</configuration>
Thanks for any suggestion and help.
I'd be tempted to comment out that configuration line on the client and host, and then trying running it. That config seems to set minimums and performance limits. If that doesn't change anything, you might try setting the performance throttling.
You could add this to the configuration and tinker with the settings until the performance of your web service smooths out. The default, for instance, for concurrent calls is 16, but if you raise that number using the ServiceThrottling, you might get better results.
<serviceBehaviors>
<behavior name="throttleThis">
<serviceMetadata httpGetEnabled="True" />
<serviceThrottling
maxConcurrentCalls="40"
maxConcurrentInstances="20"
maxConcurrentSessions="20"/>
</behavior>
</serviceBehaviors>

Installed WCF service with Installshield generated setup Error

I've just generated the installer for a WCF Service and I'm trying to use:
The structure installed at the wwwroot is the following:
http://bit.ly/11zZ85E
In the bin folder are included all dlls in which the service depends. The class that implements the service contract is defined in one of those dll. All generation and instalation proccess is all good. And I have on the IIS Manager the following:
http://bit.ly/10xBI3Y
The issue is that when I try to access the wsdl at the browser I'm only able to see:
<%# ServiceHost Language="VB" Debug="true" Service="MyApplication.ServiceImplementation.LicensingService" CodeBehind="MyApplication.ServiceImplementation.Service.vb" %>
And If I try to use the Add Service Reference from a Visual Studio project I see this:
There was an error downloading
(ServiceAddress)/$metadata
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved:
(ServiceAddress)
The remote server returned an unexpected response: (405) Method Not Allowed.
The remote server returned an error: (405) Method Not Allowed.
If the service is defined in the current solution, try building the solution and adding the service reference again.
The installer I did was generated with Installshield 2010.
Here is my Web.Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="MyApp.ServiceImplementation.Service" behaviorConfiguration="WSSecurityBehavior">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="Operations" binding="wsHttpBinding" contract="MyApp.ServiceContracts.IService" />
<endpoint binding="wsHttpBinding" name="mex" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WSSecurityBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WSSecurityBinding">
<reliableSession enabled="true" ordered="true" />
</binding>
</wsHttpBinding>
</bindings>
<!--protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
<handlers accessPolicy="Read, Script" />
<defaultDocument>
<files>
<clear />
<add value="Service.svc" />
</files>
</defaultDocument>
<httpErrors>
<clear />
</httpErrors>
</system.webServer>
</configuration>

WCF service page not available when browsing remotely

I have a service I have been trying to deploy on my server (iis7) for quite some time now. It works when I browse the .svc file from the server, but when I try to browse the .svc from another computer it complains that the webpage is not available.
I have enabled network discovery. What else could be causing this issue?
Here is my web.config:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="StudentInfoAlpha.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true">
<assemblies>
<add assembly="Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="StudentInfoServiceBinding">
<security mode="Transport">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="StudentInfoAlpha.StudentInfoService" behaviorConfiguration="StudentInfoAlpha.StudentInfoServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="StudentInfoAlpha.IStudentInfo" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="https://IPADDRESS:443/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="StudentInfoAlpha.StudentInfoServiceBehavior">
<!-- 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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<applicationSettings>
<StudentInfoAlpha.Properties.Settings>
<setting name="StudentInfoAlpha_AIR_AIR" serializeAs="String">
<value>http://IPADDRESS/AIRv2Host/AIR.svc</value>
</setting>
</StudentInfoAlpha.Properties.Settings>
</applicationSettings>
</configuration>

WCF service parser error, unable to import binding 'xxx' from namespace 'http://tempuri.org/'

I have a WCF service I am trying to run in IIS on my server. However, I have no idea what to do about this error, every fix I found from searching google didnt work. What am I missing here?
Error Message: Unable to import binding '...' from namespace 'http://tempuri.org/'.
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="MyService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true">
<assemblies>
<add assembly="Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MyServiceBinding">
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="MyService">
<endpoint binding="wsHttpBinding" behaviorConfiguration="webHttp" contract="MyService">
<identity>
<dns value="https://IPADDRESS:443"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WCFWsHttpBindingHttps.MyServiceBehavior">
<!-- 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>
<applicationSettings>
<MyService.Properties.Settings>
<setting name="MyService_MyOtherService_MyOtherService" serializeAs="String">
<value>http://path-to-service/MyOtherService.svc</value>
</setting>
</MyService.Properties.Settings>
</applicationSettings>
</configuration>
[edit] I should note that I get an error when hosting the service from IIS, on a DIFFERENT service that I have referenced. It hits line 1 of the WSDL and throws the parse error.
Just looking at your web.config
The service name and contract are the same which, although possible, is not best practice
The service name should be fully qualified (namespaces included)
The service behavior you have defined is not being referenced by your service element ( should add behaviorConfiguration="WCFWsHttpBindingHttps.MyServiceBehavior")
You should set serviceDebug includeExceptionDetailInFaults="true" so that you can get proper error messages returned.