today i have a question regarding style of WCF communication.
I sometimes refuse a bit so use things programmatically (want to have control myself) and I don't like huge. Sometimes communication between 2 program parts are needed, sometimes on the same machine, sometimes on network.
So I tried to use WCF programmatically instead of using configuration files, svcutil and so on.
If I use the following:
a) Define a contract
[ServiceContract]
public interface IMyContract
{
[OperationContract]
bool DoSomething(string something_in);
}
b) programm some code
public class MySomething: IMyContract
{
public bool DoSomething(string something_in)
{
if(String.IsNullOrEmpty(something_in)
return false;
return true;
}
}
and then programmaticly host it
Uri baseAddress = new Uri("net.tcp://localhost:48080/MySimpleService");
using (ServiceHost host = new ServiceHost(typeof(MyContract), baseAddress))
{
host.AddServiceEndpoint(typeof(IMyContract), new NetTcpBinding(), "");
host.Open();
Console.WriteLine("<Enter> to stop the service.");
Console.ReadLine();
host.Close();
and later just consume it from another program:
var binding = new NetTcpBinding();
var endpoint = new EndpointAddress("net.tcp://localhost:48080/MySimpleService");
var channelFactory = new ChannelFactory<IMyContract>(binding, endpoint);
IMyContract client = null;
try
{
client = channelFactory.CreateChannel();
bool test = client.DoSomething();
((ICommunicationObject)client).Close();
}
catch (Exception ex)
{
if (client != null)
{
((ICommunicationObject)client).Abort();
}
}
what would be the drawback?
Wouldn't it be easier to understand?
Could such a thing cause other problems?
(I'm mostly interested in this, because I think it is quite annoying to use svcutil and so one just because of a class change, which can simple handled manually, if the wcf service is only used for cumminication of own programs) So what am I missing?
Is it just a bad style to do things manually instead with large unread XML files?
WCF stands for Windows Communication Foundation. It is basically used to create a distributed and interoperable Application. WCF Applications came into the picture in .Net 3.0 Framework. This is a framework, which is used for creating Service oriented Applications. You can send the data asynchronously from one end point to another.
Much of the work of porting WCF to a cross-platform paradigm will involve re-implementing operation system libraries, particularly in areas such as socket layers and cryptography. These are the areas of WCF that are never likely to make it through a .Net Core port.
The fact that a .Net Core port has been accepted into the .Net Foundation should give the WCF community cause for optimism. There is a long way to go before they will be able to leverage all the good stuff in the .Net Core ecosystem. The sheer effort required in implementing the full framework may be the main obstacle.
Development shops that have invested heavily in WCF were left high and dry by the emerging .Net Core ecosystem. Despite some clear demand, there were no signs that Microsoft were considering putting it on the .Net Core roadmap. A discussion on GitHub was kicked off in 2016 and closed in early 2018 after things got a little over-heated.
Your question can be divided into multiple parts:
Is it OK to hard code address?
NO. In the same way we don't hard code connection strings we also don't hard code paths to resources or services (at least not absolute paths). You never know when you would need to change it for any reason - for example to test a new version of the service. Use at least simple app settings if you don't want to use full endpoint configuration.
Is it OK to hard code binding and configuration?
If you don't expect to change it very often and if you are happy with recompiling and redeploying server and client every time you change it you can hard code it. The API provides this because it is valid use case.
Is it OK to share service contracts and data contracts between client and server?
Again it depends on the way how you expect the application will grow and how you expect complexity of deployment. Sharing assembly is valid use case if you have full control over both client and server code but you must remember that it introduces tight coupling between your server and client application. Svcutil is tool which helps you generating clients from SOAP services you don't have control over (you don't have their code or they are not written in .NET) or for clients where you want to follow loose coupling with a server.
I very often use configuration with shared contract assembly myself = no svcutil.
Edit:
Anyway there is no technical drawback in coded configuration over XML configuration (some advanced configuration are even not available in XML). If a developer knows WCF he would understand both XML and code. If developer doesn't know WCF he would probably be more satisfied with the code because XML configuration may be hidden from him for a while.
It should work fine to do this programmatically. The main issue arises when you want to change something, or if an end user is required to change something. If the endpoint address or other config will change, it will be much easier to not have to recompile the code.
I would say that it would work out better to use a config file, that way the setup is kept in a separate place, and is easy to find when it's needed.
I would only opt to hardcode the config if you have good reason to want to hide it from the user (eg, you don't want them to see it/mess with it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With