Steps to host a WCF service in IIS5.1(XP) - wcf

I have developed a sample WCF service. I would like to know the steps to host this in IIS 5.1(XP)

1) You need a IIS virtual directory --> create it using IIS Manager
2) You need a *.svc file which references your service - it's a text file which must reside inside your virtual directory just created, and it would be something like:
<% #ServiceHost Service="YourNameSpace.YourServiceClass"
Language="C#" Debug="False" %>
That works if your WCF service class is in an assembly deployed to the "bin" directory below your virtual directory.
If you happen to have your actual service code in a "code-behind" file inside your "App_Code" directory (which I would not recommend), then you'd need this contents in your *.svc file:
<% #ServiceHost Service="YourServiceClass"
CodeBehind="~/App_Code/YourServiceClass.cs"
Language="C#" Debug="False" %>
3) You need your config in web.config - you need at least the <service> tag plus possibly more depending on your needs:
<system.serviceModel>
<services>
<service name="YourNameSpace.YourServiceClass"
behaviorConfiguration="MetadaTaEnabled">
<endpoint address=""
binding="wsHttpBinding"
contract="YourNameSpace.IYourService" />
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MetadaTaEnabled">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
Here, you need to decide what binding (protocol) to use.
If you do all this, and everything was successful, you should be able to browse to your virtual directory URL with IE (http://yourserver/virtualdirectory/YourService.svc) and see the "landing page" of your service.
Marc

Have a look at this article on MSDN. It has information about hosting WCF services in all versions of IIS.

Related

WCF Service Hosted on IIS10. Can browse the .svc file but the wsdl does not show up

I have a test WCF service that I hosted on IIS. I added a new application to the default website and used default app pool to host my test service. I am able to browse the .svc file from the content view in IIS and the success page along with a link to wsdl opens up on Windows IE. However, on clicking the wsdl link, a HTTP 404(Not found) error is thrown.(everything is on my localhost being accessed internally)
I have added the metadata endpoint and this is the relevant portion of my web.config file.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehaviour" name="ClassLibrary1.HelloWorldService">
<endpoint address="HelloService" binding="basicHttpBinding" bindingConfiguration=""
contract="ClassLibrary1.IHelloWorldService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:17000"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
Can someone please guide me about what I can be missing? Could it be a permissions issue or anything else?
Thanks.
There is no need to add the base address to service contract which will be provided by the IIS web server.
The default wcf application configuration enable the service metadata and we are able to access the metadata by the svc page or we directly use the following url.
http://localhost:90/Service1.svc?wsdl
Feel free to contract me If the problem still exists.

"There was no endpoint listening at" issue with a IIS Hosted WCF Service consuming another web service

I have created a WCF service which is hosted in IIS and that tries to call another web service (3rd party) to return some data. When trying to connect the service fails with the following error:
There was no endpoint listening at https://xxx (3rd party ws) that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
And this is while, my service is up (i know from my echo method) and it works successfully if it is self hosted.
I have the whole and sections copied to the model of web.config exactly as it is for the self hosting test but something still is missing.
I have been through other similar problems reported but mine is little bit specific in that the service is kind-of hosting another one and that one is causing the issue.
I can try to exlain better with a real example:
There is a simple web service here: http://www.dneonline.com/calculator.asmx which I want to wrap inside our library and provide access to via an IIS hosted WCF.
So, a class library is created (Calculator project) to with one method, add to take two int arguments and use them to call the web service add method. The webservice is referenced as a Service Reference inside the library and is being addressed inside from within the config library app.config file like below:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CalculatorSoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.dneonline.com/calculator.asmx"
binding="basicHttpBinding" bindingConfiguration="CalculatorSoap"
contract="Service.CalculatorSoap" name="CalculatorSoap" />
</client>
</system.serviceModel>
</configuration>
Then there is a WCF class library (CalcService project) which uses the first class library to enable http endpoints. Again, the app.config file includes endpoints both as for the service itself and as a client of the class library. The app.config file looks almost like this:
<configuration>
<system.serviceModel>
<services>
<service name="CalcService.Calc">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/CalcService/Calc/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<endpoint address="" binding="basicHttpBinding" contract="CalcService.ICalc">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<!-- Client endpoint, i.e. to be able to use the calculator.asmx service addressed in the class library -->
<client>
<endpoint address="http://www.dneonline.com/calculator.asmx"
binding="basicHttpBinding"
contract="Service.CalculatorSoap" name="CalculatorSoap" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I am able to test the whole thing via a console application that makes a call to the WCF service and receives an answer. The console application config file has only one client endpoint to the WCF like below:
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/CalcService/Calc/"
binding="basicHttpBinding" contract="Calculator.ICalc" name="BasicHttpBinding_ICalc" />
</client>
</system.serviceModel>
</configuration>
My question is now how I can host the WCF service inside IIS? I have tried different ways but neither one worked. My current IIS project (which doen't work) looks like this:
1-Has project references to both prevoius projects (Class Library and WCF Service) so two dll files are being added to the references:
CalcService.dll
Calculator.dll
2-Has a CalcService.svc file which creates a ServiceHost toward the CalcService:
<%# ServiceHost Language="C#" Debug="true" Service="CalcService.Calc"%>
3-Has a web.config with cliend endpoint to calculator.asmx:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CalculatorSoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.dneonline.com/calculator.asmx"
binding="basicHttpBinding" bindingConfiguration="CalculatorSoap"
contract="Service.CalculatorSoap" name="CalculatorSoap" />
</client>
<!-- some other settings -->
</system.serviceModel>
Now, when tested with a simple client to make a call to the calculator add method it fails with the following error:
There was no endpoint listening at http://www.dneonline.com/calculator.asmx that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
I don't know which message the endpoint is expecting, I could just assumed it has to be Service.CalculatorSoap as it worked before from the console application.
On the other hand, what confuses me is that a self hosted WCF also works (via http://localhost:8733/Design_Time_Addresses/CalcService/Calc/ from the config file in the WCF class library project).
I don't know what is missing here, is it something from the IIS configuration or permissions?
Or someting else like the windows firewall setting like explained in this post:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/bec3ab7a-310e-415f-b538-6d5681e5e53c/there-was-no-endpoint-listening-at?forum=wcf
Just note that since I am using a company computer, I'm not able to shut down the firewall. I can just turn on/off some of the rules.
I hope it is clear now what we are after.
We tested the solution on a cloud based machine and it worked fine. In the end it looked to be some firewall rules blocking the IIS outgoing calls and nothing was wrong in the configuration files or in the code.

Generate a svc / wsdl URL in my WCF web service

Usually when i implement a 3rd party WCF web service then i get a URL that ends with an .svc extension.
I just created a WCF Web Service in VS2010 and i'm able to run that service, but i don't see any URL in the Test Client that ends with a .svc extension.
Is there something else i need to do in order to get such a URL? Because usually from there people are able to get the WSDL also by adding ?wsdl to the end like:
http://site.com/service1.svc?wsdl
How can i generate such a URL in my Web Service?
This is what i have in 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="TestWebservice.Test">
<endpoint address="" binding="wsHttpBinding" contract="TestWebservice.ITest">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/TestWebservice/" />
</baseAddresses>
</host>
</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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Edit
I removed the mexHttpBinding endpoint from my configuration file. When i launch my Web Service now it automatically shows a URL to a WSDL:
http://localhost:8732/Design_Time_Addresses/TestWebservice/?wsdl
The / before the ?wsdl part seems to be very important. Otherwise it won't work.
But that still leads me to my last question. How can i specify the URL to the .svc file? I'd like to have a URL like: http://localhost:8732/Design_Time_Addresses/TestWebservice/Test.svc?wsdl
Edit 2
I got a bit further. I read this: http://msdn.microsoft.com/en-us/library/aa751792.aspx
So i created a Test.svc file in the root of my project with the following code:
<% #ServiceHost Service="TestWebservice.ITest" %>
Then i tried to access my svc file through the following URLs, but both didn't work for me:
http://localhost:8732/Design_Time_Addresses/TestWebservice/Test.svc
http://localhost:8732/Design_Time_Addresses/Test.svc
Is it even possible to host a svc inside Visual Studio? Or do i really need to host it in IIS first?
Edit 3 - Fixed!
I finally have it working! I re-added the mexHttpBinding to my App.Config.
I installed IIS7. Published the Web Service to a folder in my C:\ drive. Then mapped that folder in IIS and tried to request the .svc file in the browser.
At first it didn't work for me because it didn't recognize the .svc extension. Then all i had to do was enter the following cmd: aspnet_regiis.exe -i and now everything works fine.
Everything seems to be working fine now. Guess you can't request a .svc file when the service is hosted by Visual Studio. But it works when its hosted in IIS.

How can I register a WCF service programmatically within an IIS environment

Let's say we have 2 projects with following layout
Project "web"
global.asax (I thought of this destination for registration within eg void Application_Start(System.Object sender, System.EventArgs e)
web.config
Project "wcf"
DemoService.cs
IDemoService.cs
web.config would look like this
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="fooBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="wcf.DemoService"
behaviorConfiguration="fooBehavior">
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<endpoint address=""
binding="wsHttpBinding"
contract="wcf.IDemoService" />
</service>
</services>
</system.serviceModel>
</configuration>
So ... now ... somewhere (as mentioned above I thought of global.asax) I need to register, that when browsing to URI wcf.DemoServiceget's resolved and for a mex-request the wcf.IDemoService gets resolved for reading the attributes to get a WSDL.
This would usually be done by creating a .svc file and put header in the first line, e.g.:
<%# ServiceHost Language="C#" Debug="true" Service="wcf.DemoService" %>
In e.g. a console application by
var serviceHost = new ServiceHost(typeof (wcf.DemoService));
serviceHost.Open();
And combine this with a host element within the service element to specify the URI - or use another ctor-overload of ServiceHost
But I would rather go for a static registration (or any web.config registration which works for IIS 7.5) - is this possible? If so, how?
WCF 4 (.NET 4.0) offers both code based registration of services and configuration based registration of services.
Code based configuration is achieved through ASP.NET Routing by new ServiceRoute:
RouteTable.Routes.Add(new ServiceRoute("DemoService",
new ServiceHostFactory(), typeof(wcf.DemoService));
Routes are usually used with REST services but it works for SOAP services as well.
Registering service in configuration is called configuration based activation. You will define virtual .svc file in web.config:
<serviceHostingEnvironment>
<serviceActivation>
<add relativeAddress="DemoService.svc" service="wcf.DemoService" />
</serviceActivation>
</serviceHostingEnvironment>
In both cases you are defining only relative path to your service because base address is always specified by your web site hosted in IIS.

WCF Exception on Hosted Server

I have searched online for a solution, but have yet to find one that works.
I have a Silverlight application that uses a WCF web service.
Everything runs fine on my development environment.
When I publish to my DiscountASP.NET account - the web service gives me the following exception:
"Server Error in '/eLearning/Services' Application.
The type 'eLearning.Web.Services.Learning, eLearning.Web', provided as the Service attribute value in the ServiceHost directive could not be found. "
Please refer to the actual exception at:
http://www.christophernotley.com/eLearning/Services/Learning.svc
I have made "eLearning" a web application - and moved the web.config to the root directory.
I have also confirmed that in the markup for the web service, that the service property states "eLearning.Web.Services.Learning, eLearning.Web".
Any ideas as to what I am doing wrong?
Thanks.
Chris
Here is the markup for the web service:
<%# ServiceHost Language="C#" Debug="true" Factory="System.Data.Services.DataServiceHostFactory, System.Data.Services, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Service="eLearning.Web.Services.Learning, eLearning.Web" CodeBehind="Learning.svc.cs" %>
Here is the System.ServiceModel web config:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="RestBehaviorConfig">
<webHttp/>
</behavior>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="DebugEnabled">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0">
<binaryMessageEncoding/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://www.christophernotley.com/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<services>
<service behaviorConfiguration="DebugEnabled" name="eLearning.Web.Services.Learning">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
Well, in a WCF scenario, your service URL is determined by three things:
the name of the server
the virtual directory (and any subdirectories) where the svc-file lives
the name and extension of the svc file itself
In your case, the URL is
http://www.christophernotley.com/eLearning/Services/Learning.svc
So this begs the question:
is /eLearning really defined as a virtual directory?
is there a /Services subdirectory below your virtual directory?
is the name of the *.svc file correct?
where is the actual service code located? Do you have an assembly with the service implementation, and is it located in a place that is accessible to the *.svc file? (it's directory, a .\bin subdirectory)? Or is the code in a App_Code directory? Where is this directory??
UPDATE:
I'm a bit confused about your setup..... you say /eLearning/Services is an application - a virtual application defined in IIS, right?
In your Learning.svc file, you define a code-behind file of Learning.svc.cs - so does your service code exist there? (because in another statement, you mention a .\bin directory under /eLearning - is your service compiled into an assembly that's deployed to that bin directory??)