Can I create a WCF workflow using the standard Workflow activity templates? - wcf

Is it possible to create a WCF workflow using the standard (Activity template) Workflow activity templates? And, if so, where can I find some samples that DO NOT use the standard WCF service template (WCF Workflow Service template)?
Explanation: I'm trying to discover, load and run workflows at runtime, including workflows with WCF activities. Standard workflows get compiled into types (which makes them easy to discover), however the "WCF Workflow Service" template is an xamlx file, which is added as content and loaded as a manifest stream at runtime. This makes discovery at runtime difficult.
I don't think it is a requirement to use this template to create a service, as the WorkflowServiceHost can take an Activity in its constructor.
I'm trying to keep the task of developing a new WCF service to be discovered, loaded and "executed" (i.e., loaded and listening) at runtime as streamlined as possible.

I have been trying to figure out the same since yesterday and now I stumbled upon a workaround. There is no template for simple workflow (xaml) in VS 2010 when adding new item. If you create a WCF WF Application, you get xamlx. I created a Workflow Console application instead, that gave me a xaml file which I copied to my working project. Once this was done, hosting was simple using WorkflowServiceHost.
string uri = "http://localhost:8008/MyService";
WorkflowServiceHost wsh = new WorkflowServiceHost(new Workflow1(), new Uri(uri));
ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
wsh.Description.Behaviors.Add(metadataBehavior);
wsh.AddServiceEndpoint("IService", new BasicHttpBinding(), uri);
wsh.Open();

Yes it is.
This blog post describes how to use an SVC extension instead of a XAMLX and uses a compiles workflow to do so. The comments add some details how to get rid of the SVC file as well. You need to use the WorkflowServiceHostFactory as the Factory to wire things up. You can also do something similar when self hosting.

Actually I just figured out that "Activity" template in add new item is xaml so no need to create that "Workflow console application" to get a xaml file.

Related

How to add logging to WCF?

I've used the solution from here for logging (and insert to DB) REST request successfully, now i have to do the same to WCF
[LogApiRequest]
public NadlanData GetNadlanData(decimal Id, KodFamilyEnum KodFamily)
{
ClsDalByTz objByTz = new ClsDalByTz();
return objByTz.getDataFromMF(Id, DateTime.Now.Year - 5, (decimal)KodFamily);
}
I've tried to do the same by adding [LogApiRequest] to the WCF function but it did not work.
How can i implement the same solution for WCF?
The custom attribute - LogApiRequest - derives from System.Web.Http.Filters.ActionFilterAttribute. Action filter attributes are documented here - https://www.tutorialsteacher.com/webapi/web-api-filters and here - https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs.
The reason your attribute works in your web api is because asp.net or asp.net core processing pipeline supports calling custom code defined in user defined code - you are plugging custom feature in the processing pipeline.
Know more about asp.net pipeline here - https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/september/asp-net-request-processing-filtering-and-content-redirection
The reason it does not work with WCF is because that processing pipeline does not support customisation through HTTP filters.
WCF has its own way of doing logging. Check out these links - https://learn.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/tracing/configuring-tracing and https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/tracing-and-message-logging
You will find yourself editing custom configuration files most of the time.
I cannot give you a short attribute name or configuration script because it depends on type of WCF endpoint you are implementing, their name etc. The tracing and message logging link above contains some samples. Copy paste those configurations but edit file path and interface names based on what you have got code. That should work.

Task based asynchronous operation disabled in PCL Service Reference setting

I'm currently building a Xamarin based mobile application. For that project, I have created a PCL project with framework 4.5. I'm using VS 2013 as the development IDE. Now I want add a WCF service reference to this PCL. While adding service reference to this PCL project, I noticed that generation of asynchronous operation is disabled. Please check the image for more detail.
I added the BCL.Async package via Nuget to the project. But still I can't access the Task based operation from the radiobutton list (its disabled).
So is there any way to generate task based asynchronous operation in service client?
Hate to break it to you but you cannot generate Task based WCF client in Xamarin. The reason is Xamarin or Mono implements the Silverlight set which is a limited WCF implementation. As such you need to use SLSVCUTIL.exe instead(Adding a service reference in Xamarin would use this tool). The silverlight WCF client generated by SLSVCUTIL will be async based only.
All is not lost! You can easily wrap the silverlight async client into a task based client using the Task.FromAsync method.
A sample taken from the Xamarin website:
public async Task<List<TodoItem>> RefreshDataAsync ()
{
...
var todoItems = await Task.Factory.FromAsync <ObservableCollection<TodoWCFService.TodoItem>> (
todoService.BeginGetTodoItems,
todoService.EndGetTodoItems,
null,
TaskCreationOptions.None);
foreach (var item in todoItems) {
Items.Add (FromWCFServiceTodoItem (item));
}
...
}
https://developer.xamarin.com/guides/xamarin-forms/web-services/consuming/wcf/
Now if someone can figure out how to catch an Fault Exception when wrapping in Tasks that would be awesome!
I've not used Xamarin before, but I'll assume APM and maybe Tasks are actually supported in it and this is just a Visual Studio limitation. Try using wsdl.exe manually to generate code. This is the tool Visual Studio calls when you add a service reference.
You'll need to pass either newAsync (Tasks) or oldAsync (APM) through the /parameters switch.

Message headers sent on webservice calls on portable libraries (PCL)

I'm trying to move all the calls I make to webservices to an Portable Class Library (PCL) that I've just created to organize and reuse my code. The frameworks I'm targeting to are .NET for Windows Store apps; .NET Framework 4.5; Silverlight 4 and higher and WP7 and higher.
On my Win RT project I've been setting up the message headers by implementing the IClientMessageInspector interface available in the namespace System.ServiceModel.Dispatcher. But on my PCL project that interface as well as System.ServiceModel.Description.IEndpointBehavior are not available.
So I need to find out how to attach a message header / service header to my service calls from a PCL project with those targeted frameworks. Anyone has experience and/or suggestions that I should try?
Update
Just for adding more info, I've tried to create a WP8 project now and noticed that those interfaces are not available for it either. So IClientMessageInspector and IEndpointBehavior are probably not available for my PCL project because it is targeting WP8 which misses them itself.
You should be able to scope the OperationContext to the current client channel you want to work with:
using(var scope = new OperationContextScope(_client.InnerChannel)){
//More to come
}
Now that you have the operation context created for your client channel, you can add outgoing message headers:
using(var scope = new OperationContextScope(_client.InnerChannel)){
var header = MessageHeader.CreateHeader("x-client-type", "http://www.myapp.com", "WP8");
OperationContext.Current.OutgoingMessageHeaders.Add(header);
//Send message to server
}
After that, you should be able to get the header using the IncomingMessageHeaders property of the OperationContext.Current.
These are all core pieces of WCF services, so it should be available (hopefully).
Mono does have support for WCF services, but you would have to check on what they have implemented. EG: Perhaps they don't have MessageHeader.Create and you would have to use var header = new MessageHeader<string>("x-client-type"); and var untypedHeader = header.GetUntypedHeader("x-client-type", "http://www.myapp.com"); instead to create your header to add.

Dynamically create json using WCF service to feed MTDialog application

I wish to create a MonoTouch MTDialog-based app that upon launching adds a single element to the MonoTouch Dialog and then render the screen. This single element will be a JsonElement similar to this -
var je = new JsonElement ("Dynamic Data", "http://tirania.org/tmp/demo.json");
as shown in the sample application. However, instead of referencing a pre-built json file on a server, I wish to have the url point at a WCF OData service which will dynamically build the json data.
I am not sure what is the easiest way to do this. I am a bit lost after reading over the documentation. Is there some library I can include in my server-side C# app which I can then use to build out the elements and return as a json string of information?
I already have a WCF application up and running and just need to add an entry point which will call the method to create some simple json data for MonoTouch dialog to consume.
Any examples or suggestions are greatly appreciated as I am trying to get a simple solution completed by this weekend.
You're looking for a C# JSON library? How about JSON.NET http://james.newtonking.com/pages/json-net.aspx
You might also look at ServiceStack.NET -- they have a MonoTouch client.

Customizing the message contracts when creating a WCF service reference in Visual Studio 2010

Is it possible to modify how message contracts are generated when adding a WCF service reference in Visual Studio 2010? Specifically I want the request and response objects to have properties instead of public fields.
I have no control over the WCF service itself, just the client.
I don't think you can control the proxies that get generated, but you can modify them once they are built. Why do you want to change the proxy?
You can't control the code in the generated proxy. If you want, you can do the proxy generation yourself, using the MetadataExchangeClient / ServiceContractGenerator classes. They'll give you a CodeDom object containing the code which you can modify (i.e., change fields into properties). There's an example of using the MEC/SCG classes in the MSDN forum post at http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/14100bbb-9a73-4bcc-b513-088bb2e5ec65.