Month: July 2013

The operation ‘*************Async’ could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.

We were getting Below given error in all our environments except one.

Searching on net gave various reasons for this error like : DataContract if used in WCF Service then gives exception if you return with primitive type etc…

None of those criteria fit it as it was working fine in One CRM environment.

Reason
for this error was that Plugin was built against .Net framework 4.5 but servers Giving error had framework 4.0.

So to resolve
this we can either upgrade server to 4.5 (as it worked in one environment with Framework 4.5) or we can Build plugins against Framework 4.0.

“The operation ‘*************Async’ could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.”

The Web Service plug-in failed in OrganizationId: d7e722c9-6fa5-4bac-8ec3-05174cd4c8db; SdkMessageProcessingStepId: eb06f709-1ecd-e211-bd85-00155d885d81; EntityName: ******; Stage: 40; MessageName: Create; AssemblyName: ****************************, ************.Plugins, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c1b635ad77c3aa7c; ClassName: *****************; Exception: Unhandled Exception: System.InvalidOperationException: The operation ‘*************Async’ could not be loaded because it has a parameter or return type of type System.ServiceModel.Channels.Message or a type that has MessageContractAttribute and other parameters of different types. When using System.ServiceModel.Channels.Message or types with MessageContractAttribute, the method must not use any other types of parameters.

   at System.ServiceModel.Dispatcher.OperationFormatter.Validate(OperationDescription operation, Boolean isRpc, Boolean isEncoded)

   at System.ServiceModel.Dispatcher.OperationFormatter..ctor(OperationDescription description, Boolean isRpc, Boolean isEncoded)

   at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter..ctor(OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory)

   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.GetFormatter(OperationDescription operation, Boolean& formatRequest, Boolean& formatReply, Boolean isProxy)

   at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)

   at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch)

   at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)

   at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)

   at System.ServiceModel.ChannelFactory.CreateFactory()

   at System.ServiceModel.ChannelFactory.OnOpening()

   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)

   at System.ServiceModel.ChannelFactory.EnsureOpened()

   at System.ServiceModel.ChannelFactory1.CreateChannel(EndpointAddress address, Uri via)

   at System.ServiceModel.ClientBase1.CreateChannel()

   at System.ServiceModel.ClientBase1.CreateChannelInternal()

   at System.ServiceModel.ClientBase1.get_Channel()

   at *****************.BusinessProcess.EmailService.Service1SoapClient.*****************.BusinessProcess.EmailService.Service1Soap.SendSingleEMAIL(SendSingleEMAILRequest request)

   at *****************.BusinessProcess.EmailService.Service1SoapClient.SendSingleEMAIL(String userName, String password, String mobileNumber, String contenet)

   at *****************.BusinessProcess.XrmNew_Email.SendMessage(new_Email EmailContext, IOrganizationService service)

   at *****************.Plugins.PostEMAILCreateSendEMAIL.ExecutePostEMAILCreate(LocalPluginContext localContext)

   at *****************.Plugins.Plugin.Execute(IServiceProvider serviceProvider)

   at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)

   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)

Fakes Tutorial 3: Basic Shim Sample

To Begin with Basic example of Shim, We have 3 Projects and these are on same lines as that of previous blogpost “Basic Stub Sample“.

I’ve not used any System dll in this case so that it is easier to relate between differences in Stub and Shim. Projects are:

  1. BasicShim : This is simple class library with one method which is to be tested with Fakes Unit Testing
  2. ToBeShimmed : Contains code for an assembly which is referred in BasicShim Project and will be faked in unit test.
  3. BasicShimUnitTest: Unit test

 

Looking into Projects:

  1. ToBeShimmed

    Opposed to ToBeStubbed project this project does not contain an y Interface. There is just one Class “FetchRowCount”. This class will ideally contain logic which we need to fake.

Class

Here class FetchRowCount will be used in BasicShim class and method FetchNumberOfRows will be called.

 

  1. BasicShim

     

    In BasicShim Class we have method “MethodToTest” which we are interested in testing. In case of Shim we are not passing any Interface object to method, so in unit test instead of passing object we will fake the object at runtime.

     

    Class

  2. BasicShimUnitTest:

     

    Similar to previous example, firstly we’ll add reference to all the assemblies we need, Here it will be “BasicShim” for testing and “ToBeShimmed” for Faking.

     

    After that we’ll need to fake assembly TooBeShimmed for that

            Goto ToBeShimmed in Unit test project, -> References -> Right Click assembly to be faked. -> Click Add Fakes Assembly

     

    Test Method:

    In this sample First thing we’ll have to do is create a ShimContext, this ShimContext is used to fake the assemblies at runtime. Then we’ll set the fake behavior of Method.

    If it’s a static method then use <shim class>.<method>, if it is an instance method use <shim class>.AllInstances.<method> to set faking behavior.

    Now same as in any Object Oriented Paradigm, create object, call the method and get the return value.

    Finally we will assert it with our value i.e. “Average”.


     

    Above is the most basic unit test from fakes which can be done using “shim”.

    Keep reading for more posts around this.

Fakes Tutorial 2: Basic Stub Sample

This is in continuation to “Introduction to Microsoft Fakes

To Begin with Basic example of Stub, We have 3 Projects:

  1. BasicStub : This is simple class library with one method which is to be tested with Fakes Unit Testing
  2. ToBeStubbed : Contains code for an assembly which is referred in BasicStub Project and will be faked in unit test.
  3. BasicStubUnitTest: Unit test

 

Looking into Projects:

  1. ToBeStubbed

    This project Contains One Interface “IFetchRowCount” which is referred in Class “FetchRowCount”. This class will ideally contain logic which we need to fake

 

Interface

Class

Here class FetchRowCount will be used in BasicStub class and method FetchNumberOfRows will be called.

 

  1. BasicStub

     

    In BasicStub Class we have method “MethodToTest” which we are interested in testing. Here important part is that we are passing object “fetch” to method.

    If it’s not implemented like this it will be difficult to test it using stubs (Discussion on how to handle other scenarios are for other postsJ)

     

    Class

  2. BasicStubUnitTest:

     

    First thing to be done in Unit test is to add reference to all the assemblies we need, Here it will be “BasicStub” for testing and “ToBeStubbed” for Faking.

     

    After that we’ll need to fake assembly TooBeStubbed for that

            Goto ToBeStubbed project, -> References -> Right Click assembly to be faked. -> Click Add Fakes Assembly

     

    Test Method:

    Below you can see that in test we have Created the object of Stub class StubIFetchCount generated from interface, Set the return value of method and then passed it to the method to be tested.

    Finally we have asserted it with our value “Average”.


     

    Above is the most basic unit test from fakes which can be done using “stub”. It doesn’t cover much detail but gives overview of how stub works.

    Keep reading for more posts around this.

     

    Next: “Basic Shim Sample

Fakes Tutorial 1: Introduction to Microsoft Fakes

There has been a lots of buzz going around that there is no moles for VS 2012 and now we have to use Microsoft’s OWN unit testing framework.

So to clear that:

  1. MOLES WAS from Microsoft Only.
  2. Fakes is nothing but a differently packaged Moles J. At least from functionality perspective.
  3. You are welcome to use any other Unit testing framework if it fulfills your need and a compatible version is out there for VS 2012.

Now to begin with fakes

What is Fakes?

Fakes is isolation framework for replacing the part of code, which you don’t have control over (like webservices). Using fakes you can write your unit test and make your code believe that the values of uncontrolled part of code are coming from original source only.

Where to use it?

Let’s say we have a web service that counts the no. of rows in a database and send an Integer value to you. Depending on the integer value you show Less for value < 100, Average for value >=100 && value <1000 and More for value >=1000.

Now you can’t predict the value you will get after the service call for a particular condition as values in database constantly change.

In given scenario you will isolate that webservice, Mock it and make your code believe that the value its getting is from original service itself. Depending on Integer value you are giving while mocking you already know the output and you can Assert your unit test case against that.

 

Above example was one of the scenarios where you can use fakes, there can be many more scenarios like isolating your own code that you have written etc…

 

Some Fakes terms.

There are two different type of fakes that you can generate.

  1. Stubs: A stub is generally used to fake the code which is designed in a way that classes are inherited from interfaces.
  2. Shims: Shims are used to point your code at runtime to a method provided in test. So, shims can be used to modify behavior of assemblies.

 

What to use?

An excerpt from MSDN to explain all that’s there to know:

[quote]As a general guide, use stubs for calls within your Visual Studio solution, and shims for calls to other referenced assemblies. This is because within your own solution it is good practice to decouple the components by defining interfaces in the way that stubbing requires. But external assemblies such as System.dll typically are not provided with separate interface definitions, so you must use shims instead.[/quote]