How to make Simple WCF Service work in WPF Application? - c#

I've been following tutorial here and trying to host a simple REST Server using WCF. Basically, I created the WCF interface and class file as described in the tutorial:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GET/{msg}/{msg2}")]
string GetRequest(string msg, string msg2);
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/PUT/{msg}")]
void PutRequest(string msg, Stream contents);
}
and the concrete class:
class Service : IService
{
static string Message = "Hello";
public string GetRequest(string msg, string msg2)
{
return Message + msg + msg2;
}
public void PutRequest(string msg, Stream contents)
{
Service.Message = msg + msg + msg;
string input = new StreamReader(contents).ReadToEnd();
Console.WriteLine("In service, input = {0}", input);
}
}
These 2 WCF service classes work perfecting in a Console Application I created. Here is how "Main" looks like. When I submit a GET request to the Console Application, I get a 200 OK:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
}
However, when I want to use those 2 classes in a WPF Application, they don't work anymore. Here is the MainWindow class for the WPF Application. When I submit a GET Request to the WPF Application, I get error 502 BAD GATEWAY:
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var host = new ServiceHost(typeof(Service)))
{
var ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), new Uri("http://1.10.100.126:8899/MyService"));
ep.EndpointBehaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
host.Close();
}
}
}
}
How do you make those 2 WCF classes work with a simple empty WPF Application project? Why does those 2 WCF classes work with an empty Console Application project, but not an empty WPF Application Project?

Giving you a complete, thorough answer on how to properly host a WCF service properly in a WPF application is really a bit too broad, but here are some pointers.
You have a few major problems with your WPF attempt:
You're attempting to host the service on the UI thread, a big no-no in GUI design and programming. If you got it working the way you have it, you'd lock your UI and the user wouldn't be able to do anything but force-close your application.
You're handling it all in a code behind for a Window - WPF encourages the MVVM pattern, which guides you to separate concerns of how your view (window, controls, etc.) is rendered vs. what services are used/hosted/consumed.
You're attempting to block the thread by using Console.ReadLine() in a GUI application, where there is no Console listening - so Console.ReadLine() is just returning immediately (but if you did manage to block the thread, you'd be back to problem #1).
For a full tutorial on how to do what you're attempting with better design principles, see the following blog
Some highlights from that:
Create some controls (e.g. buttons that say 'Start' and 'Stop') to start and stop your service.
Wire up those buttons to the logic to start your service and stop your service respectively.
There's definitely improvements that could be made there - starting and managing the lifetime of the service, using the Commanding model in WPF, using the TPL or a BackgroundWorker to run the service in a different thread, making fuller usage of the MVVM pattern - but it's a start.

Related

C# Method in hosting application, turns into class in client application

I've been trying to create an application that can receive information from other running applications through WCF.
I've setup a void method in a separate class, created the interface, and hosted the service.
In my Host application I have the following method.
public Class ReceivingMethods : IReceivingMethods
{
Public void HelloWorld(string text)
{
MessageBox.Show(text);
}
}
and
[ServiceContract]
interface iReceivingMethods
{
[OperationContract]
void HelloWorld(string text);
}
In the client, I would like to do this:
HostService client = new HostService();
client.HelloWorld("Hello World");
client.close();
But it doesn't work and instead I have to do this.
HostService client = new HostService();
HelloWorld hi = new HelloWorld();
hi.text = "Hello World";
client.HelloWorld(hi);
client.close();
I've gotten it to work as the former previously in an Application/ASP combination, but not on this application and I cannot find any difference in the setup between the two applications.
Can anybody tell me what is required from the WCF setup to get it to work as the former?
HostService client = new HostService();
You haven't mention what endpoint or which class object to use.Typically the servicehost class must create the object of particular end point,something like below one.
using(System.ServiceModel.ServiceHost host =
new System.ServiceModel.ServiceHost(typeof(ReceivingMethodsnamespace.ReceivingMethods )))
{
host.Open();
Console.WriteLine("Host started # " + DateTime.Now.ToString());
Console.ReadLine();
}
Generally the hostservice must create an object of the class which is implementing servicecontract interface(servicename of AddressBindingContract file)
Turns out I found the issue somewhere else.
I configured the client service reference to "always generate message contracts"
Unchecking this and updating the service reference solved the issue.
I found the solution here.
https://social.msdn.microsoft.com/Forums/en-US/b9655eeb-cdbb-4703-87d8-00deac340173/add-service-reference-creates-message-contracts-requestresponse-objects-with-always-generate?forum=wcf

How can I stop a WCF Service from being recreated for each request?

I have a service that I need to run that manages long running background tasks. I am hosting this service through the WCF, but I'm running into a problem where the service keeps getting created and destroyed with every subsequent web request.
Here is the service:
[ServiceContract]
public interface IFileProcessService
{
[OperationContract]
[WebInvoke(UriTemplate = "ProcessFile?s={session}&file={fileName}", BodyStyle = WebMessageBodyStyle.Bare)]
int ProcessFile(int session, string fileName);
}
public class FileProcessService : IFileProcessService
{
private FileProcessTaskScheduler mTaskScheduler;
private TaskFactory mTaskFactory;
private FileProcessService()
{
mTaskScheduler = new FileProcessTaskScheduler(4);
mTaskFactory = new TaskFactory(mTaskScheduler);
}
public int ProcessFile(int scriptRunId, string fileName)
{
return mTaskFactory.StartNew(() =>
{
Console.WriteLine("Processing file {0} for script run {1}", fileName, scriptRunId);
Thread.Sleep(TimeSpan.FromMinutes(1));
Console.WriteLine("Completed processing file {0} for script run {1}", fileName, scriptRunId);
}).Id;
}
}
Obviously with the TaskScheduler and TaskFactory in there it doesn't really work when it gets disposed of at the end of every request.
Over in main I host the service like so:
WebServiceHost host = new WebServiceHost(typeof(FileProcessService), new Uri("http://localhost:7343/"));
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.HttpHelpPageEnabled = false;
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IFileProcessService), new WebHttpBinding(), "");
host.Open();
Console.WriteLine("Service is now running...");
Console.ReadKey();
host.Close();
Console.WriteLine("Service has stopped...");
I have tried making the members of the service static and then just wrapping their instantiation in the constructor with if checks, but at that point I think it would just be cleaner to write a separate singleton class to handle that stuff.
The WCF WebServiceHost seems to refer to the instance of the service that it hosts as a singleton, but it certainly isn't treating it as such. Is there some extra step I have to take to make the WebServiceHost NOT dispose of my object after every request?
Take a look at the ServiceBehaviorAttribute InstanceContextMode
You can see a working example of this type of WCF service here and this is where it's used in the code.

.NET Remoting and Server Activated Objects

what's the problem with the following code...
I have this Complex class:
public class Complex : MarshalByRefObject
{
public double imaginary{get;set;}
public double real{get;set;}
public void setReal(double re)
{
real = re;
}
public void setImaginary(double im)
{
imaginary = im;
}
public Complex(double im, double re)
{
imaginary = im;
real = re;
}
public void writeMembers()
{
Console.WriteLine(real.ToString() + imaginary.ToString());
}
}
Actually, there's a little more to it, but the code it's too big, and we don't use the rest of it in the context of this.
Then, I implemented a server which listens for connections:
HttpChannel channel = new HttpChannel(12345);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(SharedLib.Complex), "ComplexURI", WellKnownObjectMode.SingleCall);
Console.WriteLine("Server started. Press any key to close...");
Console.ReadKey();
foreach (IChannel ichannel in ChannelServices.RegisteredChannels)
{
(ichannel as HttpChannel).StopListening(null);
ChannelServices.UnregisterChannel(ichannel);
}
Then, we have the client:
try
{
HttpChannel channel = new HttpChannel();
RemotingConfiguration.Configure("Client.exe.config", false);
Complex c1 = (Complex)Activator.GetObject(typeof(Complex), "http://localhost:12345/ComplexURI");
if (RemotingServices.IsTransparentProxy(c1))
{
c1.real = 4;
c1.imaginary = 5;
c1.writeMembers();
Console.ReadLine();
}
else
{
Console.WriteLine("The proxy is not transparent");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
Then, I run the server, which opens a console window, and I run the client.
Instead of displaying 4 and 5 on the server window, I merely get 00, a sign that the members weren't changed.
How do I do, so the members change?
Thanks.
The problem is that you're using WellKnownObjectMode.SingleCall. As the documentation says:
SingleCall Every incoming message is serviced by a new object instance.
Singleton Every incoming message is serviced by the same object instance.
See also the documentation for RegisterWellKnownServiceType:
When the call arrives at the server, the .NET Framework extracts the URI from the message, examines the remoting tables to locate the reference for the object that matches the URI, and then instantiates the object if necessary, forwarding the method call to the object. If the object is registered as SingleCall, it is destroyed after the method call is completed. A new instance of the object is created for each method called.
In your case, the statement c.Real = 4 is a call to the Real property setter. It makes a call to the remote object, which creates a new object, sets the Real property to 4, and returns. Then when you set the imaginary property, it creates a new object, etc.
If you want this to work, you'll have to use WellKnownObjectMode.Singleton. But you might want to ask yourself if you really want such a "chatty" interface. Every time you set a property, it requires a call through the proxy to the server.
And, finally, you might consider abandoning Remoting altogether. It's old technology, and has a number of shortcomings. If this is new development, you should be using Windows Communications Foundation (WCF). The Remoting documentation says:
This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF).

Using Web API for a Windows Service to Receive Commands and Perform Tasks via Polling?

I have a project where I need to create a windows service that, when instructed via a command, will perform various tasks. This server would run on multiple servers and would effectively perform the same kind of tasks when requested.
For example, I would like to have a Web API service that listens for requests from the servers.
The service running on the server would send a query to Web API every 25 secs or so and pass to it its SERVERNAME. The Web API logic will then look up the SERVERNAME and look for any status updates for various tasks... I.E., if a status for a DELETE command is a 1, the service would delete the folder containing log files... if a status for a ZIP command is a 1, the service would zip the folder containing log files and FTP them to a centralized location.
This concept seems simple enough, and I think I need a nudge to tell me if this sounds like a good design. I'm thinking of using .NET 4.5 for the Windows Service, so that I can use the HttpClient object and, of course, .NET 4.5 for the Web API/MVC project.
Can someone please get me started on what a basic Web API woudld look like provide status updates to the Windows services that are running and issue commands to them...
I'm thinking of having a simple MVC website that folks will have a list of servers (maybe based on a simple XML file or something) that they can click various radio buttons to turn on "DELETE", "ZIP" or whatever, to trigger the task on the service.
I do something similar. I have a main Web API (a Windows Service) that drives my application and has a resource called /Heartbeat.
I also have a second Windows Service that has a timer fire every 30 seconds. Each time the timer fires it calls POST /heartbeat. When the heartbeat request is handled, it goes looking for tasks that have been scheduled.
The advantage of this approach is that the service makes the hearbeat request is extremely simple and never has to be updated. All the logic relating to what happens on a heartbeat is in the main service.
The guts of the service are this. It's old code so it is still using HttpWebRequest instead of HttpClient, but that's trivial to change.
public partial class HeartbeatService : ServiceBase {
readonly Timer _Timer = new System.Timers.Timer();
private string _heartbeatTarget;
public HeartbeatService() {
Trace.TraceInformation("Initializing Heartbeat Service");
InitializeComponent();
this.ServiceName = "TavisHeartbeat";
}
protected override void OnStart(string[] args) {
Trace.TraceInformation("Starting...");
_Timer.Enabled = true;
_Timer.Interval = Properties.Settings.Default.IntervalMinutes * 1000 * 60;
_Timer.Elapsed += new ElapsedEventHandler(_Timer_Elapsed);
_heartbeatTarget = Properties.Settings.Default.TargetUrl;
}
protected override void OnStop() {
_Timer.Enabled = false;
}
private void _Timer_Elapsed(object sender, ElapsedEventArgs e) {
Trace.TraceInformation("Heartbeat event triggered");
try {
var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(_heartbeatTarget);
httpWebRequest.ContentLength = 0;
httpWebRequest.Method = "POST";
var response = (HttpWebResponse)httpWebRequest.GetResponse();
Trace.TraceInformation("Http Response : " + response.StatusCode + " " + response.StatusDescription);
} catch (Exception ex) {
string errorMessage = ex.Message;
while (ex.InnerException != null) {
errorMessage = errorMessage + Environment.NewLine + ex.InnerException.Message;
ex = ex.InnerException;
}
Trace.TraceError(errorMessage);
}
}
}
You can do it with ServiceController.ExecuteCommand() method from .NET.
With the method you can sand custom command to windows' service.
Then in your service you need to implement ServiceBase.OnCustomCommand() to serve incomming custom command event in service.
const int SmartRestart = 8;
...
//APPLICATION TO SEND COMMAND
service.ExecuteCommand(SmartRestart);
...
//SERVICE
protected override void OnCustomCommand(int command)
{
if (command == SmartRestart)
{
// ...
}
}

connecting to ServiceContract?

Continuing to learn WCF, I'm trying to write a small program that would with a click of a button take the work from texbox1 , pass it to ServiceContract and get back its length.
Here's how far I got.
Form1.cs:
...
wcfLib.Service myService = new wcfLib.Service();
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToString( myService.go(textBox1.Text) );
}
...
and the wcf file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace wcfLib
{
[ServiceContract]
public interface IfaceService
{
[OperationContract]
int wordLen(string word);
}
public class StockService : IfaceService
{
public int wordLen(string word)
{
return word.Length;
}
}
public class Service
{
public int go( string wordin )
{
ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
serviceHost.Open();
int ret = **///// HOW SHOULD I PASS wordin TO StockService to get word.Length in return?**
serviceHost.Close();
return ret;
}
}
}
what I can't figure out right now, is how do I pass the wordin variable above into the ServiceContract?
You need to create the client in your form and call wordLen() directly... only a class that inherits from IfaceService can be called as a WCF service. So:
// You'll have to create references to your WCF service in the project itself...
// Right-click your form project and pick 'Add Service Reference', picking
// 'Discover', which should pick up the service from the service project... else
// enter http://localhost:8000/wcfLib and hit 'Go'.
// You'll have to enter a namespace, e.g. 'MyWcfService'... that namespace is
// used to refer to the generated client, as follows:
MyWcfService.wcfLibClient client = new MyWcfService.wcfLibClient();
private void button1_Click(object sender, EventArgs e) {
// You really shouldn't have the client as a member-level variable...
textBox2.Text = Convert.ToString(client.wordLen(textBox1.Text));
}
If your Service class is meant to host the WCF Service, it needs to be its own executable and running... put the code you have in go() in Main()
Or host your WCF Service in IIS... much easier!
Edit
IIS = Internet Information Services... basically hosting the WCF Service over the web.
To host in IIS, create a new project, "WCF Service Application". You'll get a web.config and a default interface and .svc file. Rename these, or add new items, WCF Service, to the project. You'll have to read up a bit on deploying to IIS if you go that route, but for debugging in Visual Studio, this works well.
To split into two applications, just make the form its own project... the service reference is set through the application's config file; you just point it to the address of the machine or website, e.g. http://myintranet.mycompany.com:8000/wcflib or http://myserver:8000/wcflib.
Thanks for the vote!
You've definitely got things back-to-front. You don't want to create the ServiceHost in your Go method, or at least, you'd never create it in any method invoked by the client, because how could the client call it if the service hasn't been created yet?
A service in WCF is started, and THEN you can invoke its methods from a remote client. EG, this is your Main() for the service:
ServiceHost serviceHost = new ServiceHost(typeof(StockService), new Uri("http://localhost:8000/wcfLib"));
serviceHost.AddServiceEndpoint(typeof(IfaceService), new BasicHttpBinding(), "");
serviceHost.Open();
Console.WriteLine("Press return to terminate the service");
Console.ReadLine();
serviceHost.Close();
Then for your client you'd use "Add Service Reference" in Visual Studio (right-click on the Project in Solution Explorer to find this menu option) and enter the address for the service. Visual Studio will create a proxy for your service, and this is what you'd instantiate and use on the client. EG:
MyServiceClient client = new MyServiceClient();
textBox2.Text = Convert.ToString( client.wordLen(textBox1.Text) );

Categories