Our application server exposes 5 WCF services over the net.tcp transport, all on the same port. We've been hosting these during development using WcfSvcHost and I've never had to think about how these manage to use the same port.
We're moving them to a Windows Service now, and now I'm instantiating the ServiceHost instances myself. One of the services uses Streamed TransferMode over Tcp.
When starting these services using a configuration file with WcfSvcHost, they work fine. But in our service it complains about the port being in use.
Should it be possible for the streamed service to use the same port?
I solved the problem eventually, after alot of trial and error with programmatic configuration of the bindings.
It seems that something in the binding stack generated when you create a NetTcpBinding allows multiple NetTcpBindings to share a port. The problem was that I needed to make a custom binding.
The Solution ended up being to create a custom binding based on a NetTcpBinding. For example:
var lBinding = new NetTcpBinding()
{
SendTimeout = TimeSpan.FromMinutes(5),
ReceiveTimeout = TimeSpan.FromMinutes(5),
MaxConnections = 100,
ReliableSession = new OptionalReliableSession
{
Enabled = true,
Ordered = true,
InactivityTimeout = TimeSpan.FromMinutes(30)
},
Security = new NetTcpSecurity
{
Mode = SecurityMode.TransportWithMessageCredential,
Message = new MessageSecurityOverTcp { ClientCredentialType = MessageCredentialType.UserName }
},
MaxReceivedMessageSize = 524288
};
var lCustomBinding = new CustomBinding(lBinding);
// Edit the custom binding elements here
var lEndpoint = new ServiceEndpoint(lContract, lCustomBinding, new EndpointAddress(pServiceHost.BaseAddresses.First()));
I found another solution to for this issue by using a the RoutingService class. Each contract must still be hosted in it's own ServiceHost, but there can be a RoutingService sitting on top of all of them - and presenting them over an unified "endpoint". I've also written a codeproject article about it. The example code is also available on Bitbucket.
See here about Net.TCP Port Sharing, which is what you're looking for.
You've got to enable the service for that too.
Related
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
I have a Silverlight 5 application that is obtain data from a Silverlight-enabled WCF service in another project (my solution has a Silverlight project and a web project). There are a number of similar posts, but they're referring to web services hosted on a web server (proper web services).
I am aware that I have to tell the application where to find the service in both dev and prod. In dev, you'll have a port typically. My code to create a service client (with the correct EndpointAddress) is as such:
BasicHttpBinding binding = new BasicHttpBinding(
Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase)
? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
Uri tempUri = new Uri("../PolicyDataService.svc", UriKind.Relative);
EndpointAddress servAddr = new EndpointAddress(tempUri);
PolicyDataServiceClient temp = new PolicyDataServiceClient("BasicHttpBinding_PolicyDataService", servAddr);
return temp;
Now, where am I going wrong? In dev, everything works fine, but in prod the service is never being called. Thanks!
I'm not sure what's going on, but here are a few things that you can try if you haven't already:
Browse to the svc file on Prod and make sure you can access it (http://yoursite/PolicyDataService.svc)
Point your code to the prod location and make sure you can connect without any issue. Doing this will usually reveal what the problem is, provided you can connect.
Since you're using BasicHttpBinding, you shouldn't be running into security issues with the binding. I know this is pretty basic, but have to start somewhere.
Calling a WCF Service in my application throws EndpointNotFoundException after one minute. All timeouts are more than one minute.
var binding = new BasicHttpBinding {
OpenTimeout = TimeSpan.FromMinutes(3),
CloseTimeout = TimeSpan.FromMinutes(6),
ReceiveTimeout = TimeSpan.FromMinutes(2),
SendTimeout = TimeSpan.FromMinutes(5)
};
client = new ServiceClient(binding, new EndpointAddress("http://..."));
client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(4);
I found a thread on Microsoft's forum, but there is no solution.
http://social.msdn.microsoft.com/Forums/ar/windowsphone7series/thread/cba9c633-6d79-4c04-8c08-cd0b5b33d8c6
The problem occurs only with services that work out more than one minute.
Invoke of this service throws EndpointNotFoundException:
public string Test() {
Thread.Sleep(60000);
return "test";
}
But invoke of this service works correctly:
public string Test() {
Thread.Sleep(58000);
return "test";
}
It is not clear from the question if the problem occures on the emulator or the device.
If it is occuring on the emulator do you have network access - i.e. can you see external sites from IE. If not check the proxy settings on your host machine as a LAN proxy will prevent the emulator communicating.
What are the server-side timeouts set to? Sounds like the issue may possibly be at the other end of the wire.
I downloaded .NET Framework's libraries from Windows Phone device and decompile they.
HttpWebRequest has unchangeable timeout in 1 minute.
To confirm, I created an aspx page. If I put Thread.Sleep(60000) in Page_Load, HttpWebRequest will not be able to get an response.
I have a problem with WCF NetNamedPipeBinding. When I run my server and client code through Visual Studio 2008 on a Windows XP machine everything works fine. But as soon as I deploy my server as a Windows Service and install my client app in Windows Server 2008 I get a TimeoutException on the client end whenever I try to use any of the contract methods. It seems that I can successfully create the client and open it, but can't call any of the methods.
Service initialisation code:
Uri baseAddress = new Uri("http://localhost:8500/xNet/xNetService");
string address = "net.pipe://localhost/xNet/xNetService";
_xNetAPIServiceHost = new ServiceHost(typeof(xNetService), baseAddress);
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
_xNetAPIServiceHost.AddServiceEndpoint(typeof(IServiceAPI), binding, address);
// Add a mex endpoint
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8501/xNet/xNetService/mex");
_xNetAPIServiceHost.Description.Behaviors.Add(smb);
_xNetAPIServiceHost.Open();
Client initialisation code:
string address = "net.pipe://localhost/xNet/xNetService";
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
_serviceClient = new ServiceAPIClient(binding, new EndpointAddress(address));
_serviceClient.Open();
The Windows service runs as "Local System Account". I'm at a loss as to what the problem is. I don't know if it's a security account problem, or if the named pipe is even open? I would assume since I can successfully create and open the client side it would appear it at least found the named pipe. I just can't call any of the service methods without a TimeoutException.
After trying out various bindings and going back to basics I noticed that the sample programs worked, but mine didn't work unless I was using Visual Studio to debug. I decided at that point that it must be something going on with my own code. To simplify debugging I turned off all security in the binding.
I started commenting out most of the statements in my service's OnStart method in order to determine what could be going on. I commented out everything except for the code that initialises the ServiceHost. Magically, my client could now successfully communicate with the service. I then started uncommenting each line of code in the OnStart method until my client suddenly started giving me a TimeoutException again.
My service class, say "MyAPI", implements the contract "IMyAPI". As well as using "MyAPI" class as the WCF service, I was also using an instance of the "MyAPI" class internally in my service to do various things ("internal" methods). In my OnStart method I first created an instance of the "MyAPI" class and then created the ServiceHost:
MyAPI API = new MyAPI();
ServiceHost service = new ServiceHost(typeof(MyAPI));
I was not getting any errors or exceptions, so it appeared everything is fine, but really I couldn't connect to the service using a client. As soon as I changed the order of the above statements, the client started working again:
ServiceHost service = new ServiceHost(typeof(MyAPI));
MyAPI API = new MyAPI();
I'm not sure WHY this is occuring, all I know is that I can use my API internally and as a service without any client connection issues. Maybe someone out there will provide some light on the reasons behind this, or maybe my code is not properly designed.
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