How do I solve the error in my WCF service and proceed on? - wcf

I am currently developing a C# Windows Form Application that I intend to let it interact with a server. The server will receive posting from a mobile application that I have developed and whenever a posting is received, my Windows Form Application should be notified and give me a notification.
E.g. My mobile application sends an message over to my server. Once my server receives the message, my windows form application should display a new notification showing the content of the message received.
I am now starting to develop my WCF Service and this is what I've done so far
[ServiceContract(Namespace = "Posting")]
public interface IPostingService
{
[OperationContract]
void NotifyAboutPosting(Posting post);
}
[DataContract]
public class Posting
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public DateTime PostingTimestamp { get; set; }
}
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost/");
ServiceHost selfHost = new ServiceHost(typeof(Posting), baseAddress);
try
{
// Step 3 of the hosting procedure: Add a service endpoint.
selfHost.AddServiceEndpoint(
typeof(IPostingService),
new WSHttpBinding(),
"Posting");
// Step 4 of the hosting procedure: Enable metadata exchange.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
// Step 5 of the hosting procedure: Start (and then stop) the service.
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
// Close the ServiceHostBase to shutdown the service.
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
}
}
regarding the posting class, what I want to ask is that are the methods inside used to get the information from the server?
and how do I proceed on from here after the service is done. (My winform application has been finished and all thats left is to add in this logic to receive the posting whenever the mobile app sends to the server.
and there seems to be a compilation error of
The contract name '##.IPostingService' could not be found in the list of contracts implemented by the service '##.Posting'.
could anyone help me with this? thanks a million!

Where is your actual implementation? You have contract (IPostingService), data (Posting)... but where's code doing the work? You seem to lack contract implementation:
public class PostingService : IPostingService
{
public void NotifyAboutPosting(Posting post)
{
// do something with post here
}
}
And you register actual worker class (not data) when setting up your host:
ServiceHost selfHost = new ServiceHost(typeof(PostingService), baseAddress);

I would suggest checking out these likely culprits:
WCF - Contract Name could not be found in the list of contracts
Discussion on Microsoft forums
Blog post on the issue

Related

How to add SoapEndPoint after server is started on ASP.NET Core?

I'm using simple console app to expose a soap web service. It works as expected.
Now i want to add another web service after the server is started. How to make it work?
I have following simple console application:
static void Main(string[] args)
{
var host = WebApplication.CreateBuilder();
_App = host.Build();
_App.UseRouting();
_App.UseEndpoints(endpoints =>
{
endpoints.UseSoapEndpoint<ISimpleServiceInterface>("/SimpleService.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});
_App.Urls.Add("http://*:5000");
_App.RunAsync();
Console.WriteLine("Server has been started successfully ...");
AddNewService();
Console.ReadLine();
}
Server starts and i can access the wsdl http://localhost:5000/SimpleService.asmx?wsdl)
Now the AddNewService method simple try to define a new SoapEndPoint after service started.
Code looks like this:
static private void AddNewService()
{
try
{
System.Threading.Thread.Sleep(5000); // Wait 5 seconds to make sure web application is running
Console.WriteLine("Adding new service ..."); // Add new Soap service now, after startup
_App?.UseEndpoints(endpoints =>
{
endpoints.UseSoapEndpoint<ISimpleServiceInterface2>("/SimpleService2.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});
Console.WriteLine("Added new service.");
}
catch(Exception ex)
{
Console.WriteLine("Failed to Add new service. Error=" + ex.Message);
}
}
This works ok if first request to url is done after the service is created: (http://localhost:5000/SimpleService2.asmx?wsdl)
But if a request is sent before the service is created. Then any request done after the creation of the service will fail:
I'm guessing i need to raise some event or something to the web server to get it refreshed or something.
How can i do that?
Also is there a way to remove a SoapEndPoint once is has been defined/exposed?
Idea is basically being able to add/remove/update SoapEndPoint on the fly.
Any help will be appreciated. Thanks in advance
Progess a little bit on this.
I basically need to register IActionDescriptorChangeProvider class to be able to notify the web application.
I also needed to slightly change my main routine.
Here is the main function:
static void Main(string[] args)
{
var host = WebApplication.CreateBuilder();
host.Services.AddControllers();
host.Services.AddSingleton<IActionDescriptorChangeProvider>(MyActionDescriptorChangeProvider.Instance);
host.Services.AddSingleton(MyActionDescriptorChangeProvider.Instance);
host.Services.AddSingleton<ISimpleServiceInterface, SimpleService>();
_App = host.Build();
_App.MapControllers();
_App.UseRouting();
_App.UseEndpoints(endpoints =>
{
endpoints.UseSoapEndpoint<ISimpleServiceInterface>("/SimpleService.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});
_App.Urls.Add("http://*:5000");
_App.RunAsync();
Console.WriteLine("Server has been started successfully ...");
AddNewService();
Console.ReadLine();
}
Then the AddService function (note the 2 lines added to make the notification):
static private void AddNewService()
{
try
{
System.Threading.Thread.Sleep(5000); // Wait 5 seconds to make sure web application is running
Console.WriteLine("Adding new service ..."); // Add new Soap service now, after startup
_App?.UseEndpoints(endpoints =>
{
endpoints.UseSoapEndpoint<ISimpleServiceInterface2>("/SimpleService2.asmx", new SoapEncoderOptions(), SoapSerializer.XmlSerializer);
});
// Notify the web application of the changes
MyActionDescriptorChangeProvider.Instance.HasChanged = true;
MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
Console.WriteLine("Added new service.");
}
catch(Exception ex)
{
Console.WriteLine("Failed to Add new service. Error=" + ex.Message);
}
}
and class implementing IActionDescriptorChangeProvider:
public class MyActionDescriptorChangeProvider : IActionDescriptorChangeProvider
{
public static MyActionDescriptorChangeProvider Instance { get; } = new MyActionDescriptorChangeProvider();
public CancellationTokenSource TokenSource { get; private set; } = new CancellationTokenSource();
public bool HasChanged { get; set; }
public IChangeToken GetChangeToken()
{
TokenSource = new CancellationTokenSource();
return new CancellationChangeToken(TokenSource.Token);
}
}
Once you do that, it will work fine on the second request (wsdl request).
Problem is that the wsdl may be accessible but the function itself (route to the actual method on the singleton) is not there.
Registration of the singleton for ISimpleServiceInterface2 need to be done but not sure how to achieve this.
My end goal is to be able to add/remove/update soap web service after server is built.
Basically idea is to update the soap service with a newer assembly.
If anybody has some idea, comments, response, please post them here. That will be appreciated.

WCF Winform freezes when opened by server

I've written a VSTO Outlook Add-In and am trying to open a form in a separate Winform app when the user pushes a button on the Add-In, passing an integer as argument. I'm using WCF Named Pipe Binding. The Add-In is the client and the Winform app is the server. The binding and inter-process communication works fine. However, when the target form opens, it freezes with a spinning cursor. The form otherwise works fine from within the Winform app.
Per the code below, when I call "clsActivity.EditT("", activityID);" within the server method, it opens a form which is properly created and displayed, but then locks up with a spinning cursor and is inaccessible. I've been assuming that there is some element of the servicehost or other WCF process that is uncompleted, but can't identify the issue.
I've spent several days trying to find an answer. As I'm pretty new to WCF, I'm not even sure if I'm asking the right questions. Any assistance would be greatly appreciated. Thanks.
Server
// Service Contract
[ServiceContract(Namespace = "ComsIPC")]
interface ComsIPCContract
{
[OperationContract(IsOneWay = true)]
void OpenActivity(int activityID);
}
// Server Implementation
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class ComsIPCServer : ComsIPCContract
{
public void OpenActivity(int activityID)
{
try
{
clsActivity.EditT("", activityID);
}
catch
{
MessageBox.Show("Error in ComsIPCServer");
}
}
public void CreateComsIPCServerHost()
{
string address = "net.pipe://localhost/coms/IPC";
ServiceHost serviceHost = new ServiceHost(typeof(ComsIPCServer));
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
serviceHost.AddServiceEndpoint(typeof(ComsIPCContract), binding, address);
serviceHost.Open();
}
}
Client
// Service Contract
[ServiceContract(Namespace = "ComsIPC")]
interface ComsIPCContract
{
[OperationContract(IsOneWay = true)]
void OpenActivity(int activityID);
}
public class ComsIPCClient
{
public void OpenActivityInComs(int activityID)
{
string address = "net.pipe://localhost/coms/IPC";
NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
EndpointAddress ep = new EndpointAddress(address);
ComsIPCContract channel = ChannelFactory<ComsIPCContract>.CreateChannel(binding, ep);
channel.OpenActivity(activityID);
}
}

Closing WCF Connections from service

I have recently started a new job where WCF services are being used. I have used them in the past and am comfortable with them but from what I can recall if the client does not close the connection it has the ability to bring your service down entirely. I am aware of the proper procedure for closing the connections but if the responsibility is on the client, they may not follow the same practices and potentially have the ability to bring the service down. Is there any other way of handling the closing of the connections so that it is not reliant on the client doing the right thing? It seems odd that anyone who has access to your service has the ability to bring it down with such ease...
Thank you very much for any insights!
One option is to use session time out in the server. This actually faults the client channel.
There are only really three ways in which a session can terminated:
1) The client closes the proxy
2) The service's receiveTimeout is exceeded before the client sends another request
3) The service throws a non-fault exception which will fault the channel and so terminate the session
If you don't want the client involved then you only have 2 and 3 neither of which end well for the client - they will get an exception in both situation on the next attempt to talk to the service.
You could use Duplex messaging and get the service to notify the client that its requires session termination - the client then gets an opportunity to close down the proxy gracefully but this is a cooperative strategy
Or you need to use duplex (but still the client will have to call the service).
Here is some important points of the service implementation:
a: Use a static dictionary to keep the Client’s IP and callback channel. Before writing on the share object, lock the object.
b: Gets the IP address of the client using the GetAddressAsString method. You can get the IP of the client from the incoming message. The following statement shows how can we get the IP adddress of the Client in WCF:
RemoteEndpointMessageProperty clientEndpoint = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
String ipAddress = clientEndpoint.Address;
If you are using the namepipe binding, you will not get the RemoteEndpointMessageProperty.
c: When the client creates the proxy of the service, it will call StartingService method immediately. Inside the StartingService method, I am keeping the callback channel of the client and current instance into the dictionary.
d: When the user of WCF service wants to disconnect a client, he/she will call the Disconnect method with the IP Address of the client.
e: The Disconnect method uses the IP Address to get the callback channel of the client and associate service instance of the client from the dictionary. Eventually, it notifies the client by using callback channel and close the incoming channel.
Here is the implementation through code:
[ServiceContract(CallbackContract=typeof(INotifyClientCallback),SessionMode=SessionMode.Required)]
public interface IService1
{
[OperationContract]
bool StartingService();
}
public interface INotifyClientCallback
{
[OperationContract(IsOneWay = true)]
void Disconnecting();
}
INotifyClientCallback interface for Callback.
Step 2: Implementation of the Contact:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
private static readonly Dictionary subscribers = new Dictionary();
public static event EventHandler onClientAdded;
///
/// Returns the IP Address of the Client
///
///
public string GetAddressAsString()
{
if (!OperationContext.Current.IncomingMessageProperties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
return "127.0.0.1";
}
RemoteEndpointMessageProperty clientEndpoint =
OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
return clientEndpoint.Address;
}
public bool StartingService()
{
//Get the callback reference
INotifyClientCallback callback = OperationContext.Current.GetCallbackChannel();
string IPAddress = GetAddressAsString();
lock (subscribers)
{
if (!subscribers.ContainsKey(IPAddress))
{
subscribers[IPAddress] = new CommunicationStore()
{ NotifyCallback = callback,
IService = OperationContext.Current.InstanceContext
};
if (onClientAdded != null)
{
onClientAdded(IPAddress, null);
}
}
}
return true;
}
public static void Disconnect(string ipAddress)
{
if (subscribers.ContainsKey(ipAddress))
{
CommunicationStore com = subscribers[ipAddress];
if (((ICommunicationObject)com.NotifyCallback).State == CommunicationState.Opened)
{
try
{
//fires the callback method
com.NotifyCallback.Disconnecting();
com.IService.IncomingChannels.FirstOrDefault().Close();
}
catch (Exception)
{
throw;
}
}
}
}
}
public class CommunicationStore
{
public InstanceContext IService { get; set; }
public INotifyClientCallback NotifyCallback { get; set; }
}

How can I throttle a WCF MSMQ endpoint?

I'm new to WCF and just learning how to get a client to talk to a host (both in console applications) using MSMQ.
I want to be able to send messages from client to host and have the host pick them up immediately or, if the host is stopped, to continue where it left off when it is restarted.
I've got this almost working but I find that when I restart the host with ten messages in the queue, the messages are not processed in the queue order. I assume there's some multithreading going on that makes them appear out of order. I'd like to be able to limit the WCF service to processing one message at a time to stop this happening (unless there's a better solution).
It's essential to a system that I'm about to work on that the MSMQ messages are processed in order and not in parallel.
The code for my service contract is:
[ServiceContract(Namespace = "http://www.heronfoods.com/DemoService")]
public interface IDemoService
{
[OperationContract(IsOneWay = true)]
void SendMessage(string message);
}
For the Service contract implementation I've got this. (The console output is because this is a demo app for me to learn from):
public class DemoService : IDemoService
{
public void SendMessage(string message)
{
Console.WriteLine("{0} : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
}
}
My host application is a console application with the following code:
class Program
{
static void Main(string[] args)
{
Console.Title = "WCF Host";
using (var host = new ServiceHost(typeof(Library.DemoService)))
{
var endpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof(Library.IDemoService)),
new NetMsmqBinding(NetMsmqSecurityMode.None),
new EndpointAddress("net.msmq://localhost/private/test"));
host.AddServiceEndpoint(endpoint);
host.Open();
Console.WriteLine("Host Active");
Console.ReadLine();
}
}
}
The client is equally simple:
class Program
{
static void Main(string[] args)
{
Console.Title = "WCF Client";
IDemoService proxy = ChannelFactory<IDemoService>.CreateChannel(
new NetMsmqBinding(NetMsmqSecurityMode.None),
new EndpointAddress("net.msmq://localhost/private/test")
);
do
{
string msg = Console.ReadLine();
if (msg=="")
break;
else
proxy.SendMessage(msg);
} while (true);
}
}
I am assuming your queue is not transactional.
While I'm not certain there's a way to throttle netMsmqBinding to a single thread, you shouldn't need to apply this restriction.
To guarantee ordered delivery you only need to make your queue transactional and then apply the exactlyOnce attribute to the netMsmqBinding configuration.
See example here.

How do I program my WCF service in the given scenario?

I am currently developing a C# Windows Form Application that I intend to let it interact with a server. The server will receive posting from a mobile application that I have developed and whenever a posting is received, my Windows Form Application should be notified and give me a notification.
E.g. My mobile application sends an message over to my server. Once my server receives the message, my windows form application should display a new notification showing the content of the message received.
I am now starting to develop the WCF service and has reach the PostingService method and I am unsure of how I am able to continue to program the service to work the way I wan as stated above.
public class PostingService : IPostingService
{
public void NotifyAboutPosting(Posting post)
{
// do something with post here
}
}
and after I program the service, how do I test the service by, I dunno? uploading a fake post to see if the services works or whatsoever , meaning a dummy test. thanks !
EDIT
for my main method, the code are as follows ,
Uri baseAddress = new Uri("http://localhost/");
ServiceHost selfHost = new ServiceHost(typeof(IPostingService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(IPostingService),new WSHttpBinding(), "Posting");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
basically I just followed through the tutorial given by MSDN WCF getting started tutorial. not sure if this is really the correct way to do it for the type of implementation that I want.
Well, your WCF service can do anything you want - so what do you really want it to do??
Your posting server gets a new message from a mobile device, and then calls this WCF service class in your Winforms app. What do you want to happen here and now??
One thing to keep in mind: the WCF service class receiving the message and the Winforms app might be running on different threads; if that's the case, you cannot just update e.g. UI elements on your Winforms UI from the service code (you need to use some synchronization methods). But that depends on your exact way of creating and opening the ServiceHost in your Winforms app.
Update: if you put your code to create and initialize your ServiceHost into the main application form (see Service Synchronization Context on CodeIdol for a sample on how to do this), then you could probably just do:
public class PostingService : IPostingService
{
public void NotifyAboutPosting(Posting post)
{
MessageBox.Show(post.Title, post.Message,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
assuming your Posting class has both a .Title and a .Message string property...
1) PostingService assembly (class-library project)
Interface: IPostingService.cs
using System;
using System.ServiceModel;
namespace PostingService
{
[ServiceContract]
public interface IPostingService
{
[OperationContract]
void NotifyAboutPosting(Posting posting);
}
}
Implementation: PostingService.cs
using System;
using System.Windows.Forms;
namespace PostingService
{
public class PostingService : IPostingService
{
public void NotifyAboutPosting(Posting posting)
{
MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
DataContract: Posting.cs
using System;
using System.Runtime.Serialization;
namespace PostingService
{
[DataContract]
public class Posting
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public string Message { get; set; }
}
}
2) Your Winforms app (Winforms application project)
Must reference the service assembly (since it needs the service contract and the data contract class)
Main Form of your app: Form1.cs
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;
using PostingService; // your class library from above
namespace WinformsApp
{
public partial class Form1 : Form
{
private ServiceHost _host = null;
public Form1()
{
InitializeComponent();
// IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
_host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
_host.Description.Behaviors.Add(smb);
_host.Open();
label2.Text = "Service up and running (http://localhost:8888/PostingService)";
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
_host.Close();
base.OnFormClosed(e);
}
}
}
3) Run your Winforms app - now that service is up and running and ready to be notified.
4) Launch WCF Test Client (that's what your "posting server" will be doing later on)
4a) File > Add Service - type in http://localhost:8888/PostingService - should find your service
4b) if found: enter some values into the properties for the "Posting" class (an ID, title, message)
4c) Click "Invoke" - your service should now be called, a dialog pop (message box) should pop up with the title and message you've defined
Perhaps WCF callbacks might meet your requirements:
What You Need To Know About One-Way Calls, Callbacks, And Events