WCF- how to add multiple services to Service.svc? - wcf

I have 4 services running via a service host project, which communicate fine with my asp.net application when the ASP.NET development server hosts them through VS for debugging.
I am trying to deploy these to IIS on a windows server 2008 machine, using WAS.
I have the project set up as an application in IIS, and have copied the entire config section from app.config in servicehost project to web.config of the IIS site.
After a few compliation issues, I now get a directory listing when i navigate to http://localhost:8000/Services
I have also created a Service.svc file, which contains
<%#ServiceHost Service=MyApp.AddressService %>
When I navigate to localhost:8000/Services/AddressService, I get a message that i've created a service, and appending ?wsdl gives me the xml to create a client.
Problem is, I get an error when I try to add any more services to the .svc file.
Should I be using service.svc to configure multiple services, or is there a different way using WAS?
How can I expose my other three services through the same application?
Thank you!

You cannot add multiple service to a SVC file. One SVC file = one service class. No way to change that.
However: you can definitely implement multiple service interfaces on your service class:
public class YourService : IService1, IService2, IService3
{
...
}
and then you have one SVC file = one service (implementation) class = 3 service contracts.
In .NET 4 / WCF 4, you'll be able to define URL's for service in your web.config, and you don't need the SVC files anymore.
See this blog post here or this one here for more info, if .NET 4 is an option for you.

Related

Cannot fetch wcf service remotely

i have 2 systems, one is localhost(Windows 8.1Pro) and another is Virtual Machine(Windows server 2008R2).
i have a silverlight app hosted in VM which is consuming WCF service from localhost installed in windows service.
Problem:
silverlight is not getting clientaccesspolicy.xml and crossdomain.xml so wcf service will not fetched.
Tried solutions:
Added clientaccesspolicy.xml and crossdomain.xml in WCF service solution before installing but still it can't be fetched from WCFservice url.
Added clientaccesspolicy.xml and crossdomain.xml in C:\inetpub\wwwroot folder, as per my search so many suggested to place their which will be automatically fetched by silverlight.
tried adding clientaccesspolicy.xml code into wcf service as per this link still no success.
Any suggestions will be appreciated.
Edit:
okay i am attaching code FYR, i am doing this as below.
once i build WCFTest , 'll get .exe wcf installer which i am installing that using installutil.exe, so service'll be up and running in services.msc.
once i build TestSL, 'll get TestSLTestPage.html , on double clicking on this it'll open in IE, and on button click i should get "Hello World" from WCF.
Download it from here and suggest me if i am doing anything wrong. Attached projects are of exact same structure as i am workinig on....
Regards,
Jithendra
as i explained my prob.above i was using self hosted Wcf service and i couldn't solve the problem so Here's what i did
1. Created Default WCF Service application in VS2013 with all my code and added clientaccesspolicy.xml and crossdomain.xml in the solution.
2. Created a new website in IIS Manager to publish my project.
3. and published my project to IIS as filesystem.
4. There we'll have all the files that are necessary.
And Voila everything works perfect.
Hope it helps somebody.

Programmatically configure and host WCF Service in IIS

How can i programmatically configure and host WCF Service in IIS. I have created my WCF service example /WCFServices/Service1.svc". I want to programmatically configure and host this service in IIS. Can anyone help me on this?
The class you want is Microsoft.Web.Administration.ServerManager
http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager(v=VS.90).aspx
It has methods for manipulating most aspects of IIS, for example, adding application pools and applications. for example, this code configures a new IIS application
//the name of the IIS AppPool you want to use for the application - could be DefaultAppPool
string appPoolName = "MyAppPool";
//the name of the application (as it will appear in IIS manager)
string name = "MyWCFService";
//the physcial path of your application
string physicalPath = "C:\\wwwroot\mywcfservice";
using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
ConfigurationElement siteElement = sitesCollection[0];
ConfigurationElementCollection siteCollection = siteElement.GetCollection();
ConfigurationElement applicationElement = siteCollection.CreateElement("application");
applicationElement["path"] = name;
applicationElement["applicationPool"] = appPoolName;
ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
virtualDirectoryElement["path"] = #"/";
virtualDirectoryElement["physicalPath"] = physicalPath;
applicationCollection.Add(virtualDirectoryElement);
siteCollection.Add(applicationElement);
serverManager.CommitChanges();
}
In general, the calss is just a thin wrapper around the IIS config file. You can understand it by looking at your exisiting file, or even by looking at what you have to do in IIS Manager to configure the service manually, then translating that into the resulting configuration changes.
You can do all (at least lots of) the the IIS configuration in this way (e.g. configure application throttling, enable authentication schemes etc.).
The WCF part of the configuration is just normal WCF. you can do it either in code or in configuration.
What you are looking for is called Publish. You can find it from the right-click context menu on the WCF Service project. You can publish from there or create a package for publishing later or distributing it to a remote site. There are a lot of tutorials on the net.
If you have a specific question about this feature, feel free to ask.
Have a look at msdeploy, a command line packaging and deployment tool:
http://blogs.iis.net/msdeploy/
http://vishaljoshi.blogspot.de/2009/02/web-deployment-with-vs-2010-and-iis.html
http://msdn.microsoft.com/en-us/vs2010trainingcourse_webdevelopment_topic8#_Toc282089433

Using ChannelFactory with WorkflowServiceHost

Is there a way to do the service inference on a workflow definition XAML to create an interface that can be distributed to the client to be used with ChannelFactory instead of the host exposing WSDL and the client having to generate a service definition by adding a service reference?
I did this in a three step process:
Temporarily exposing the metadata from the workflow service
Creating proxy code with svcutil
Changing configuration back to not exposing metadata
In detail:
Include your XAMLX file which defines the service in a project that was created as "WCF Workflow Service Application" (DeclarativeServiceLibrary1)
Compile the project
Set the project as startup project
Select xamlx file in Solution Explorer
Press Ctrl-F5 -> WCF Test Client starts, you see your service loaded
RightClick on the xamlx URL in WCF Test Client, choose Copy address (e.g. http://localhost:56326/Service1.xamlx)
Open a VS2010 Admin console window
Create the proxy code with svcutil.exe:
cd /D %TEMP%
svcutil http://localhost:56326/Service1.xamlx
This creates two files, a *.cs and a *.config, that contain the proxy code
I had a lot of problems with other ways of craeting the proxy code (inside VS2010), the external svcutil was the most stable way to do it. Hope this helps.

WCF service reference in class library

I added wcf service (exposed by biztalk) proxy and app.config file in VS 2005 class library project and i am calling this class library methods from windows application. In this case getting error while creating service instance, so i moved app.config file from class library to window applcation, now working fine.
Question: If i will change service url from machine001 to machine002 in config file (from bin folder but not from application) and run the app from exe file. Will it work without build.
Class library configuration always depends on configuration file (web.config / app.config) of parent application that is really using it. And Parent application should be a console / winform / ASP.NET application and can be a windows service. Any change in WinForm's app.config will change the behavior of your class library.
To Answer your question, Yes if you change service url from machine001 to machine002 in config file of windows application it will work if machine002 is hosting the WCF service.
Hope it clears your doubt.

Connection string in app.config in a class library

I am creating solution and inside I have three projects:
A WCF Service Library Project
A DataAccess Project (Class Library)
A Web site for hosting WCF service
The implementation of the service is on the project # 1, but in order to access the DataBase I use a second project that implements the data access using a class library project.
That problem is in order to get data access I need to configure a connection string, but that connection string must be configurable in a production environment, I meant in production I am going to deploy the site, which is a very simple project that contains only a reference WCF Service Library Project then a guy from database department will configure the connection string.
In development I have an app.config on the data access project but when I do the release that app.config is embedded on the dll.
Any ideas how can we achieve our purpose
The connection string must be in the application configuration file of the executing assembly. This means that you can provided the configuration file for your assembly along with the assembly itself but anyone who wants to use your assembly must update their configuration file to include the values that your assembly relies on.
The connection string in your app.config (data layer) is not embedded in the dll.
If you look in the app.config file in your data layer project, you will probably have a connectionStrings section. you need to put the connectionStrings in the web.config of your WCF service website.
This can be configured in your production environment.
I had a mistake, I was using a different name on the web.config of the WCF site, I just copy the the exact part of the app.config to the web.config and its working now.
Thanks for your help