How to host Web API in Windows Service - wcf-web-api

I have several resources that I'd like to expose using the WCF Web API. I've investigated the Web API using a Web host but our services all run as Windows Services in production so it's time for me to put the tests aside and verify that everything will work as we need it. I've looked as the sample app here: http://webapicontrib.codeplex.com/SourceControl/changeset/view/2d771a4d6f6f#Samples%2fSelfHosted%2fserver%2fProgram.cs but this does not work with the current version (preview 5) because the HttpConfigurableServiceHost class is not accessible from our code.
One of the most appealing aspects of the Web API is the simple startup using MapServiceRoute and the new WebApiConfiguration. I don't see, however, a way to define the base url and port for the services. Obviously, hosting the service in IIS eliminates this because we configure this information in IIS. How can I accomplish this when hosting in a Windows Service?

It's actually pretty simple. In a nutshell you need to instantiate HttpSelfHostServer and HttpSelfHostConfiguration and then call server.OpenAsync().
public void Start()
{
_server.OpenAsync();
}
public void Stop()
{
_server.CloseAsync().Wait();
_server.Dispose();
}
For an example on how to do this using Windows service project template and/or Topshelf library see my blog post: http://www.piotrwalat.net/hosting-web-api-in-windows-service/

The latest version just uses HttpServiceHost. http://webapicontrib.codeplex.com/SourceControl/changeset/view/ddc499585751#Samples%2fSelfHosted%2fserver%2fProgram.cs
Ping me on twitter if you continue to have problems.

This is the basic code using a console app. A Windows Service uses the same basic approach except you use the start and stop methods to start and stop the service and don't need to block.
static void Main(string[] args)
{
var host = new HttpServiceHost(typeof(PeopleService), "http://localhost:8080/people");
host.Open();
foreach (var ep in host.Description.Endpoints)
{
Console.WriteLine("Using {0} at {1}", ep.Binding.Name, ep.Address);
}
Console.ReadLine();
host.Close();
}
See this blog post.

Related

Getting the Base URL of the application inside of the ASP.NET Core HostedService

I need to obtain the base URL of the ASP.NET Core application inside of one of the HostedServices.
I need this because it does a request to the same ASP.NET core application in which it is hosted (the purpose is warming up, to improve the first call performance to the User).
For now my solution is to keep the base URL in the config file or just in the hosted service private variable.
https://github.com/BBGONE/JRIApp.Core/blob/master/DEMOS/RIAppDemoMVC/RIAppDemo/Utils/WarmUpService.cs
But i think, there's a way to obtain it from the startup code, but i don't know where it is hidden.
Anybody know how it can be obtained?
P.S. - there are solutions to obtain it from the request information, but the HostedService is started before any request have been done. So it's not suitable in this case.
I have found how to obtain the address of the appllication.
public void Configure(IApplicationBuilder application)
{
var addresses = application.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
}
Although it has an issue https://github.com/aspnet/Hosting/issues/811 and can not be used if the application is hosted in the IIS or IIS Express.
They say:
That's not going to work for IIS or IIS Express. IIS is running as a
reverse proxy. It picks a random port for your process to listen on
and does not flow the public address information to you. The only way
to get the public address information is from incoming requests.
The ASP.NET Core Module generates a dynamic port to assign to the backend process. CreateDefaultBuilder calls the UseIISIntegration method. UseIISIntegration configures Kestrel to listen on the dynamic port at the localhost IP address (127.0.0.1). If the dynamic port is 1234, Kestrel listens at 127.0.0.1:1234. This configuration replaces other URL configurations provided by.
But if you get the feature from the WebHost after it was built, then this can be used to get the local address for warm up.
I tried this way:
public static void Main(string[] args)
{
var builder = CreateWebHostBuilder(args);
var webHost = builder.Build();
var addresses = webHost.ServerFeatures.Get<IServerAddressesFeature>().Addresses;
var address = addresses.FirstOrDefault();
AppDomain.CurrentDomain.SetData("BaseUrl", address?? "");
webHost.Run();
}
and got the local Kestrel address in the WarmUpService like this:
string baseUrl = AppDomain.CurrentDomain.GetData("BaseUrl").ToString();

Web Api documentation with swashbuckle

We are currently trying to start writing WebApi services for our products switching from traditional WCF SOAP based services. The challenge we have got is how to provide the api documentation. I came across the SwaggerUi/swash buckle.
One limitation we have is we do not want to host the WebApi services in IIS but in a Windows Service. I am new to Web Api so I might be doing things the wrong way.
So for testing, I am hosting the web api in a console application. I can use the HttpClient to invoke the Get method on the Web Api but I can't access the same if I type the url in a web browser (is this normal for self hosted web api?).
So I installed the Swashbuckle.core nuget package and included the following code in the Startup class (Owin selfhosted).
var config = new HttpConfiguration();
config
.EnableSwagger(c =>
{
c.IncludeXmlComments(GetXmlCommentsPath());
c.SingleApiVersion("v1", "WebApi");
c.ResolveConflictingActions(x => x.First());
})
.EnableSwaggerUi();
private static string GetXmlCommentsPath()
{
var path = $#"{AppDomain.CurrentDomain.BaseDirectory}\WebApiHost.XML";
return path;
}
When I browse to the following location
http://localhost:5000/swagger/ui/index
I get "page cannot be displayed" in IE. Similar for chrome.
Is there anything special that needs to be done when hosting a WebApi in a console/windows service application to get the documentation automatically?
(I have enabled Xml documentation for the project)
I have now attached a test project. Please follow the link below:
Test project
Regards,
Nas
Your problem is not with Swashbuckle, which is configured correctly. Instead it is with the fact that your OWin web app has closed by the time the browser navigates to the swagger URI. Your using statement means that the web app is shut down at the end of it - well before Chrome has opened and navigated to the swagger path. You need to ensure the web app is still running - then your path will be valid (although in your source code you have different ports 9000 and 5000 in your url variables).

WCF Client Configuration in a Sharepoint Webpart

I have a Sharepoint 2010 webpart that calls a WCF service.
I've created a service proxy and manually coded the endpoint, see below.
In a conventional WCF client I'd use the config files for the configuration and use transforms when I was buiding for deployment to different environments.
How would I achieve the same through a Sharepoint webpart? I want to put the configuration somewhere that it can be changed for different build configurations.
ie. For a local deployment during testing, then a test server, production. We're trying to automate this as much as possible.
Thanks,
Tim
UPDATE:
I'm aware that you need to put config data in the web.config file in sharepoint. I'm looking for a way to put these config settings into source control and have them automatically populate / deploy for different builds and environments.
namespace CombinedPortal.WcfClient {
public class FrameworkServiceProxy : IFrameworkService
{
private IFrameworkService _proxy;
public FrameworkServiceProxy()
{
var endpoint = new EndpointAddress("http://server:1234/FrameworkService.svc");
var binding = new WSHttpBinding(SecurityMode.None);
_proxy = new ChannelFactory<IFrameworkService>(binding, endpoint).CreateChannel();
}
public Framework GetCurrentFramework(double uniqueLearnerNumber)
{
var fw = _proxy.GetCurrentFramework(uniqueLearnerNumber);
return fw;
}
} }
Your code is C# code which executes on the server.
When then user presses a button on a web part there is a POST back to the Sharepoint web server, where the C# code executes.
It is therefore the web.config of your SharePoint site which is used.

use of RIA Service project's DLL in Window service

I have one project which has RIAService with entity framework that is referenced to my Silvelright project. so when i build project that build RIA Service project's DLL and put that in Bin/Debug folder of Silverlight project.
Domain service function is like below
[EnableClientAccess()]
public partial class MyClassDomainService : LinqToEntitiesDomainService<MyDatabaseEntities>
{
[Invoke]
public void MyFunction(int Para1, string Para2, int Para3, string Para4)
{
//mycode
}
}
Now i am build this porject and copy this RIAService.dll and other require DLLs to my another project is window service project.
Now in window service i am creating object of this Domain service as like below in timerQlinkRequest_Elapsed() function.
private void timerQ_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
MyClassDomainContext objcontext = new MyClassDomainContext();
objcontext.MyFunction(1, "ADD", 1234, "Vehicle");
}
catch (Exception ex)
{
LogMessage("Error : StackTrace : " + ex.StackTrace);
}
}
After installing and Start window service i get below error in ex.stackTrace in log filem as below
Error : StackTrace : at MS.Internal.JoltHelper.get_Context()
at MS.Internal.XcpImports.Application_GetCurrent(IntPtr& pApp)
at System.Windows.Application.get_Current()
at System.ServiceModel.DomainServices.Client.WebDomainClient`1.ComposeAbsoluteServiceUri()
at System.ServiceModel.DomainServices.Client.WebDomainClient`1..ctor(Uri serviceUri)
at RIAService.Web.Service.QLink.MyClassDomainContext..ctor()
at MywindowService.MyService1.timerQ_Elapsed(Object sender, ElapsedEventArgs e)
so my main confusion is, can i do like this? if yes then where i go wrong?
please help me.
waiting for reply.
You shouldn't be doing what you're trying to do. WCF RIA was created to bridge the gap between specific implementations of server and client applications. When you build your project, there's all kinds of code-generation going on that ties the client to an implementation on the server.
Now, what you CAN do here is:
Move your second client to the same solution and add it as a second RIA client to the Silverlight.Web project. You can do this from the project settings and you'll get full RIA benefits.
You can try to call your deployed RIA service, as if it is a WCF service; after all it is called WCF RIA Services for a reason. Now you won't get all the built in support (mostly provided by the code generation) but you can still manually perform CRUD operations through WCF actions. Check here "Browse to the Domain Service Directly" section,
to see how to locate the svc files.
I recommend the 1st option, if you're going for Edit capabilities. But 2nd option should work relatively painlessly for read-only data. But then again for that I'd recommend exposing an O-Data endpoint, which is very easy to do in WCF RIA.
i get solution in other way,
in my project.Web (ASP.NET project) i create a simple WCF application that call my function of WCF RIA Service.
And in window service solution I add service reference of that WCF service and calling that WCF function and complete my task.
#duluca thanks fro the reply.

Windows service using WCF NetNamedPipe to communicate to client

The reason I'm writing this question is that I seem to be getting the following error when I'm trying to communicate between a windows service and a WPF app via a WCF service with a NetNamedPipe binding:
System.ServiceModel.EndpointNotFoundException: There was no endpoint
listening at net.pipe://localhost/Pipe_SendInfo
Now the gory details.
Ok, I have a windows service that is periodically executing code, I wanted to let a user know what is happening inside the service. So I read that I could accomplish this via NetNamedPipe WCF service. I created two test apps and successfully was able to send a message from one process to another. I then attempted to send messages from the windows service to a client app(on the same machine) and have so far failed miserably :(.
My windows service essentially does this(trying to send info):
ChannelFactory<SkipSyncLib.ISendInfo> pipeFactory =
new ChannelFactory<SkipSyncLib.ISendInfo>(
new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/Pipe_SendInfo"));
pipeFactory.CreateChannel();
pipeFactory.SendInfo(info);
and the application that is supposed to receive the information does this when it starts up:
public void Start()
{
HostService = new ServiceHost(this, new Uri[] { new Uri("net.pipe://localhost") });
HostService.AddServiceEndpoint(typeof(ISendInfo), new NetNamedPipeBinding(), "Pipe_SendInfo");
try
{
HostService.Open();
}
catch (Exception exc)
{
//Error handling
}
}
The kicker is that while the windows service is failing miserably to find the endpoint I have another console app that is able to send info to the running client app successfully. So logic would tell me that it probably has something to do with users. But I can't figure it out.
Any ideas? Should I just drop named pipes and go with an http binding?
I just found out about named pipes earlier today so please be gentle if the answer is obvious.
Thanks
I figured out a way for it to work. There's probably a more elegant way to do this, but if the windows service and the client app are run as the same user then the communication channel works.
Should you use the same endpoint address?
HostService = new ServiceHost(this, new Uri[] { new Uri("net.pipe://localhost/Pipe_SendInfo") });
add /Pipe_SendInfo in Uri