How to get the virtual path to where WCF is hosted on IIS rather than the physical path? - vb.net

I have a WCF Service hosted on IIS server, however i try to get the path to which the service is hosted on the IIS, that is similar to: 192.168.1.xx/WCFHosted/VirtualDirectory, by using the below code:
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath
The above returns a path to the Physical path corresponding to where the IIS Hosted WCF is originally linked from, that is Ex: D/MyService/WCFHosted,
How can i return the IIS Hosted path for the WCF,
Please Advise,
Thanks in Advance

Try the below code snippets.
System.Environment.MachineName+"/" + System.Web.Hosting.HostingEnvironment.SiteName +System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
We can convert the Machine name to an IP address by using DNS.GetHostAddress(hostname) method.
var result = Dns.GetHostAddresses(Environment.MachineName).ToList();
foreach (var item in result)
{
Console.WriteLine(item);
}
Feel free to let me know if there is anything I can help with.

Related

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

SVC client proxy and WSDL hosted on remote server

I have one wsdl and xsd then I generated a SVC client proxy from a wsdl, wsdl is hosted on windows server 2003 (outside local domain), somewhere on internet. XSD is also located at same place. I can see them in web browser, no issue there.
Then I made a simple console application client, when I run the client, I see in fiddler that correct soap response is there but in console application response is null, i.e. SVC client can not deserialize it.
I am sure that xsd + wsdl are correct and response too(I see in fiddler).
When I put wsdl and xsd's on local domain server(intranet), it's all good, I see response in application properly deserialized.
If I compare both situation in fiddler, I don't see any difference in raw response.
Few more points: I am not hosting WCF service in IIS. I have got a different service running remotely. Let me know if I clarify more things.
Thank you.
Edit/ Update:Cleaned post. Problem seems not to be IIS6 or IIS7, it's remote server creating trouble with schema location. In SVC proxy client MessagContract namespace if pointed to a outside server, problem occurs else on intranet server it runs just fine.
Example:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class responseTypeOfMethod1
{
[System.ServiceModel.MessageBodyMemberAttribute( Namespace = "http://testserver.xxxxx/yyyy.wsdl", Order = 0)]
public RAT_type RAT;
................
....
..
}
if http://testserver.xxxxx points to local server, all fine, if points to an internet server , response is null
There are 2 things that could be happening here:
The response is not being returned from the remote server
The client is not able to deserialize the response
Use wireshark or some other network sniffer to see what response in coming back.
If the problem is due to inability to deserialize, it is probably a difference in the namespace.
Problem solved. It was namespace problem of-course, took a long time to find out a nasty place in server code, a tool we use internally. Thank you all who replied for your time.

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

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.

WCF / Silverlight Call Back to Server Fails in IIS

Using Silverlight 3, Windows XP, IIS 5.1, I've written a small app which uses the channel method of calling the server rather than the 'add service reference' as per this MSFT article.
The application opens and the call to the server work when running it on the development computer in VS 2008 using the address localhost plus the port number. When I change the address to the computer name, dellnov2006, and publish the application to IIS, the application opens, but the call to the web service does not work.
Watching the call in Web Dev Helper, I see that the app was trying to call the service file, http://dellnov2006/Service1.svc, and is getting a 404 error.
So far, I've:
-In IIS mapped the .svc type to aspnet-isapi.dll
-Run the utility CleanIISScriptMaps
-Run aspnet_regiis.exe -i –enable
Any help would be appreciated - I am running out of ideas on this.
--
Here is the call back to the server, and the contents of the Service1.svc file:
private void Button_Click(object sender, RoutedEventArgs e)
{
// create a custom binding that uses HTTP and binary encoding
var elements = new List<BindingElement>();
elements.Add(new BinaryMessageEncodingBindingElement());
elements.Add(new HttpTransportBindingElement());
var binding = new CustomBinding(elements);
// create a channel factory for the service endpoint configured
// with custom binding
//var cf = new ChannelFactory<IService1>(binding,
// new EndpointAddress("http://localhost:1042/Service1.svc"));
var cf = new ChannelFactory<IService1>(binding,
new EndpointAddress("http://dellnov2006/Service1.svc"));
// save the syncronized context for the ui thread
uiThead = SynchronizationContext.Current;
// open the channel
IService1 channel = cf.CreateChannel();
// invoke the method asychrnoously
channel.BeginGetPerson(4, GetPersonCallback, channel);
}
Here are the contents of the svc file for what they are worth:
<%# ServiceHost Language="C#" Debug="true" Service="SilverlightChannelApp1.Web.Service1" CodeBehind="Service1.svc.cs" %>
Many thanks
Mike Thomas
Could be one of the following:
A problem with the web.config of the service. For example that localhost was part of the address.
That the service cannot find the dll which should be in the bin directory
Try browsing to the service with a web browser
Try adding the port number to the computer name. Whenever I'm testing local sites through a virtual machine that is always a necessity for me.
Change this:
new EndpointAddress("http://dellnov2006/Service1.svc"));
To this:
new EndpointAddress("http://dellnov2006:1042/Service1.svc"));
The solution to this was very simple, but it took both of your answers for me to think of
it.
Browsing to the service as suggested by Shiraz worked, so problem with calling service.
Suggestion to change endpoint address to include port # sounded good, but did not work.
Solution was to change:
new EndpointAddress("http://dellnov2006/Service1.svc"));
to this:
new EndpointAddress("http://dellnov2006/Silverlight/Service1.svc"));
where 'Silverlight' is the alias of the virtual directory. In other words, I open the app on IIS as 'http://dellnov2006/Silverlight/
Many thanks, I cannot believe how simple that was after so much time spent looking. I work alone and if it were not for this forum I'd be in serious trouble.
Mike Thomas

Amazon Elastic Cloud and Silverlight Deployment

We are experimenting with hosting a silverlight application on Amazons EC2.
I can get it to serve up the .xap file, but I'm having some trouble with using the webservices that the silverlight application requires.
Usually I would add a service reference in visual studio and enter the URL for the webservice, something like http://url.com/ServiceName.svc and a proxy would be generated for me.
However with the Amazon Elastic Cloud instance entering the url
http://ec2-174-129-139-48.compute-1.amazonaws.com/AuthService.svc
Gives the error "is not recognised as a known document type"
And if I enter
http://ec2-174-129-139-48.compute-1.amazonaws.com/AuthService.svc?wsdl
Into the internet explorer address bar I get a wsdl description - but it has this part in the config which seems a bit odd
<wsdl:types>
<xsd:schema targetNamespace="http://asp.net/ApplicationServices/v200/Imports">
<xsd:import schemaLocation="http://ip-0af8db15/AuthService.svc?xsd=xsd0" namespace="http://asp.net/ApplicationServices/v200" />
<xsd:import schemaLocation="http://ip-0af8db15/AuthService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
</xsd:schema>
</wsdl:types>
The schemaLocation http://ip-0af8db15/AuthService.svc? doesn't look like the right address to me?
Anyone know if I need to configure something or change something to access WCF webservices on Amazon EC2?
Edit: Should note : Windows Server 2003, IIS 6.0
Edit: Looks like ip-0af8db15 is the machine name
Just in case someone went into the same problem:
on proxy generation try remove the http:// at the beginning of the EC2 Address and then in the Service Config replace the part "ip-0af8db15" with the EC2 Address.
Hope it helped.
I have the same scenario deployed with no issues. Why don't you try using the IP address instead of the dynamic hostname:
http://174.129.139.48/AuthService.svc
Edit:
If an unreachable server name is being put into the VS.Net generated proxy then you can adjust it manually in the automatically generated configuration.svcinfo. Alternatively you can set the URL programatically, this is a better option since it won't get over written if you need to re-generate the proxy.
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://174.129.139.48/AuthService.svc");
YourProxy yourProxy = new YourProxy(binding, address);
This thread (particularly the last two posts) helped me to solve this problem.
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fd51a2-773e-41d4-95a0-244e925597fe/