WCF Workflow Service REST interface [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
RESTful Workflow Service Endpoints in WF4 / WCF
I am trying to make Windows Workflow Services 4.0 work with a REST interface. I have a very simple workflow service called "Service1" with a receiveRequest and sendResponse activity.
By default WF Services autogenerate the classes and interfaces implemented, however i would like to force the WF Service to use my own REST enabled interface instead of some internal autogenerated interface.
The interface would be the following:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke( UriTemplate = "/Data/{item}", Method = "GET" )]
String GetData( Int32 item );
}
However, i have difficulties configuring the XAML to work with this interface.
I would need a XAML configuration like this to specify that the Service contract name is my own contract:
<Receive x:Name="__ReferenceID0" CanCreateInstance="True" DisplayName="ReceiveRequest" sap:VirtualizedContainerService.HintSize="464,90" OperationName="GetData" ServiceContractName="w:IService">
However when i run this workflow service i get the following exception:
The contract name 'wfService.IService' could not be found in the list of contracts implemented by the service 'Service1'.
However, the service that gets created behind the scenes does not implement the IService interface and i would like to know how can i extend the service that gets instantiated by the workflow engine to implement my own interface (which i described above)?
Thanks

In WF4 you cannot declare ServiceContract in code and use it. Contract is declared in XAML and WorkflowServiceHost generates endpoint from declaration.
To enable REST for for your workflowservice you have few options:
Use HttpWorkflowHost from http://wf.codeplex.com/wikipage?title=WebAPIWorkflow. Limitation is that then you will have only REST.
Do something similar to this: http://msdn.microsoft.com/en-us/library/aa967564.aspx
Differences are: replace WorkflowFormatterBehavior instead of DataContractSerializerOperationBehavior, arguments are extracted from message contract instead of operation contract and keep in mind that you will not have client part of this example and you will have to format response according to protocol.

Related

WFC Service Not Seeing Operation Contract

I am having a weird problem where I have a wcf service that has some Operation Contracts but when I add the service reference to another project they are there.
When I go to add -> add service reference. I put in the wfc url and the service shows up.
When I look at the operations list I see those endpoints but when I hit "ok" and then I try to find those endpoints in my project they are not found.
How can I go about debugging this?
Make sure you are using the correct Data Anotations on your interface, for example:
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(ExceptionMessage))]
List<student> GetStudents();
[OperationContract]
[FaultContract(typeof(ExceptionMessage))]
void AddStudents(Student student);
[OperationContract]
[FaultContract(typeof(ExceptionMessage))]
void DeleteStudent(long StudentId);
}
You must have a [Service Contract] interface. And it should contain your methods
When you successfully add the service reference, the proxy class will be automatically generated in the project:
And it will automatically generate web.config, web.config contains endpoint information:

Can I use a web api from a web application as a service reference?

Is it possible to add as a reference and call an APIs controller methods as a service on another project? What are the alternatives if this is not possible?
Web API types of applications do not have a 'service reference' anymore. They do not produce WSDL, so you cannot add them like you used to do with SOAP services. No proxy classes are generated... no intelli-sense.
Web APIs are typically called with lightweight http requests and return JSON and not XML based SOAP responses like traditional ASMX or SVC (WCF) services.
You have some reading to do I believe.
To answer your question, you CAN indeed call API services from a web application (say a controller method in an MVC app), but you won't have proxy classes to help you.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
When you create a service reference you end up with a reference to an interface and a client class that implements the interface.
You can follow pretty much the same pattern without a WCF service reference. In fact, that's one of the benefits of depending on an interface. It doesn't matter to your application whether the implementation is a call to a WCF service, an API, or anything else.
First declare an interface that describes how you will interact with the API.
public interface ISomethingService
{
public SomeData GetSomeData(string id);
}
That interface is what your other classes depend on. They'll never know what the implementation is.
Your implementation could be something like this. I'm using RestSharp to create the API client because I like it better than managing an HttpClient:
public class SomethingServiceApiClient : ISomethingService
{
private readonly string _baseUrl;
public SomethingServiceApiClient(string baseUrl)
{
_baseUrl = baseUrl;
}
public SomeData GetSomeData(string id)
{
var client = new RestClient(_baseUrl);
var request = new RestRequest($"something/{id}", Method.POST);
var response = client.Execute<SomeData>(request);
return response.Data;
}
}
In your startup you would register this class as the implementation of ISomethingService and pass the base url from configuration. That would also allow you to pass a different url for development, production, etc. if needed.
Ultimately it's no different from depending on a WCF service. One difference is that a WCF service defines an interface, but in this case you have to do it. That's actually a good thing, because it's better for your application to define its own interface rather than directly depending on the ones someone else provides. You can wrap their interface or API in a class that implements your own interface, giving you control over the interface you depend on.

WCF ServiceHost instantiation confusion with multiple endpoint contracts

I am a bit confused on what exactly I am instantiating with the WCF ServiceHost when I have multiple endpoint contracts that I am adding to it.
The instantiation has included a typeof argument - which seems to be the contract and is so in everything I have read and done. However when I come across adding additional contracts - this is where my confusion about it is.
ServiceHost shost = new ServiceHost(typeof(MyService), NetTcpBinding, xyz);
So let's say I have several contracts - ProductService, BatchService, CustomerService these are endpoint contracts that each have an interface. Let's keep it simple there is an Add Method and a Get Method in each of these contracts.
I can then add these endpoints which are contracts to the ServiceHost ..
shost.Endpoint.Add(ProductService);
shost.Endpoint.Add(BatchService);
shost.Endpoint.Add(CustomerService);
This is my confusion if I create it with MyService, then does MyService need to incorporate the methods of all of my endpoint contracts or does this just pass in the first Endpoint Contract just to instantiate it and then all the additional ones are (forgive my lack of a better way of saying this) - additional services provided by the service that was instantiated with one of my endpoints ?
I have read on SO and looking here seems relevant and close - but does not give an explanation of the instantiation of the ServiceHost
Run WCF ServiceHost with multiple contracts
I mean what is the point of instantiating the thing ; and then adding endpoints if you have to place all of the endpoint methods into the host contract anyway where btw you can specify namespaces for the contract as well..- that just seems so un oop .. is the answer found at the link really the viable answer (it smells WET ~ W'peat Every Thing - AKA not DRY].
The ServiceHost can host one service - that is one service class (implementation class). But that single class can implement multiple WCF service contracts.
So if you have three service contracts (as interfaces IProductService, IBatchService, ICustomerService) and a single class MyServiceClass which implements all three of those interface contracts
public class MyServiceClass : IBatchService, ICustomerService, IProductService
then you can host this class in a ServiceHost and you can define endpoints for each of those three service contracts.

Combine ADO.NET Data Service and custom ServiceContract in the same class?

I have an ADO.NET Data service that I run through WCF:
public class AdminService : DataService<BOPApplicationAccessEntities> {
public static void InitializeService(DataServiceConfiguration config) {
config.UseVerboseErrors = true;
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("*", EntitySetRights.All);
}
}
I'd like to add some custom methods to it, such as the following contract
[ServiceContract]
public interface IAdminService {
[OperationContract]
void RequestAccess(int applicationID, string username);
}
If I add the decoration and implement the method on the data service, an error is thrown when a client tries to connect, saying:
AdminService implements multiple servicecontract types, and no endpoints are defined in the configuration file.
Is it not possible to add service contracts onto an ADO.NET data services service?
The error states that the service exposes multiple contracts (interfaces), which is true, because you just added a new one. The host is not able to work based on implicit default values anymore, because it does not know which service interface to host on which endpoint.
Make sure you have an explicit endpoint defined in your service configuration file for each contract your service implements. Thing will start to work for you again after that, though you might need to update the service reference in your client application after making the modifications.
Update: Combine How to disable authentication schemes for WCF Data Services which tells how to explicitly create an endpoint for your WCF Data Service with http://www.west-wind.com/weblog/posts/2008/Apr/10/WCF-REST-Configuration-for-ASPNET-AJAX-and-plain-REST-Services to troubleshoot problems with your specific error message.

event based model client and ChannelFactory<T>

Hallo again my question regards to event based model client development in WCF service client infrastructure. what I would lice to know is that, instead of using ClientBase is it possible to manually implement this patten with ChannelFactory so for example I could write GetDataAsync for client access and still using ChannelFactory and implementing serverside async calls here?
The answer will depend on whether you control the service contract or not. If you can define the service contract then you add the appropriate begin/end methods that return/use the IAsyncResult as shown in the code in this blog post.
If you can't change the service contract then you must create an async version of the service contract manually and feed that to the ChannelFactory. The ChannelFactory itself does not provide an async mode of service operation invocation. Your code will end up looking something like the code in this MSDN sample.

Categories

Resources