Can we override methods in WCF or web services? - wcf

I want to know if we can override WCF methods or web service methods. If so, then how?

Try this for overloading WCF service methods
[ServiceContract]
public interface IMyCalculator
{
[OperationContract(Name="AddFloats")]
float Add(float operand1, float operand2);
[OperationContract(Name="AddIntegers")]
int Add(int operand1,int operand2);
}

Method overriding is not possible in WCF you can only implement the method overloading as explained in this posts.
Even I giving the code below for method overloading:
[ServiceContract]
public interface IMyCalculator
{
[OperationContract(Name="AddFloats")]
float Add(float operand1, float operand2);
[OperationContract(Name="AddIntegers")]
int Add(int operand1,int operand2);
}

Related

Calling a Web Service using WCF channel Factory.Is it possible?

In the project I am working on I need to call webservices (asmx).I would like to call them using wcf and using the channelfactory(No adding service Reference).
Some might have an interface(contract)many dont.
Is there an end to end example how to do it?
var service=ChannelFactory<?>... How do I get the webserviceContract.
Surely this must be a common scenario to be able to call a webservice (asmx)
Thanks for your time
To expand upon my comment, you should be able to create an interface that has methods that match the web service methods in the asmx service. For example:
Web Service Methods
string GetMessage()
void SendMessage(string message)
int AddNumbers(int x, int y)
Service Contract
[ServiceContract]
public interface IServiceName
{
[OperationContract]
string GetMessage();
[OperationContract]
void SendMessage(string message);
[OperationContract]
int AddNumbers(int x, int y)
}
ChannelFactory
ChannelFactory<IServiceName> serviceFactory =
new ChannelFactory<IServiceName>(new BasicHttpBinding(),
"http://www.services.com/Service.asmx");
Not 100% sure this will work, but it would be easy to try out. Also, you'd probably want to set the namespace on the service contract ([ServiceContract(Namespace = "somenamespace")]) to match the legacy asmx service, otherwise the messages might not get processed.

WCF OperationContract

What is a WCF OperationContract? I dont really understand what it does
WCF uses an opt-in model to define what belongs to one of its contracts. In a service contract interface, only methods decorated with [OperationContract] are exposed to the client. That means that, in the interface below, if used within a WCF service, a client could call both Add and Subtract operations, but not Multiply.
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int x, int y);
[OperationContract]
int Subtract(int x, int y);
// Not decorated
int Multiply(int x, int y);
}
Every method you want to be able to the user calling from his client side must be declared like that.

How to split WCF service's Operation contracts into Concurrency mode as Single and Multiple

I have one service Service A with 2 operation contract CheckServer and AddService. As the Service is singleton with Concurrey mode as Single
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
public Class Service : Iservice
{
public bool CheckService()
{
//Checks server avilabality and returns bool value
}
public int AddService(int a, int b)
{
return int i = a + b;
}
}
Here my requirement is only one Instace of AddService to be allowed, so I made this as singleton. Now CheckServvice is not necessary to be Singleton so how can I split these 2 method implementation to make AddService as singleton and CheckService as multiple.
Thanks in Advance
WCF doesn't provide what you want. Put that logic outside of WCF and write your own synchronization logic. For example implement singleton class exposing CheckService and AddService where synchronization will be directly in AddService method and CheckService method will be free to call.
Make standard WCF per-call service delegating processing to your singleton class.

Can Wcf service contract also be used by others(nhibernate asp.net)

I was wondering about if wcf would be kinda break down if mixed in with IRepository
because 2 different sources are going to be using the same contract:
- 1 being used by WCF
- another by Asp.net Nhibernate
So i wanted to reuse the same contract rather making another replica with one or 2 things out.
Easier understood by an example...
[ServiceContract]
public interface ITutorialService
{
[OperationContract]
void AddTutorial(Tutorial newTutorial);
[OperationContract]
List<Tutorial> GetTutorials();
[OperationContract]
void RemoveTutorial(string id);
Tutorial GetTutorialModel();
Tag GetTagModel();
Video GetVideoModel();
IRepository<Tutorial> GetTutorialRepository();
IRepository<Tag> GetTagRepository();
IRepository<Video> GetVideoRepository();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TutorialService : ITutorialService
{
private IRepository<Tutorial> _tutorial;
private IRepository<Tag> _tag;
private IRepository<Video> _video;......
in short would the wcf work fine as i didnt add any [OperationContract] to the Irepository ?
Yes it will work. The methods without [OperationContract] won't be WCF operations, but it sounds like that's what you want.
Another approach would be to have two interfaces and have one derive from the other so that you have separation of concerns, but do not have to define the methods twice.

Extension methods in a data contract

Can we two WCF services where one service contract derives from another and have an extension method inside the derived contract. what will be the result of accessing this contract from the WCF Client. I.e. what will happen if IDServiceis accessed
E.g.
[ServiceContract]
public interface IBaseService
{
public void A();
...
}
[ServiceContract]
public interface IDService: IBaseService
{
public static void B(this IBaseService S);
....
}
You can't define static methods in an interface (nor the access modifier public which you've specified above either).
A good question - got me to a lot of head scratching.
Extension method is meaningless to WCF - and WSDL for that matter.
If you use Service Reference to generated the client, you would not see the extension method (since WSDL would not know anything about the extension method) so you cannot use it.
If you use DLL/Project reference, your code will be called locally and not through the proxy.