Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

Posted on by 150

Hello Every One 
           I am trying to call a wcf webservice in dynamic CRM Workflow, i am getting the following error 
"Type 'SMS' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

Here is my full method

using System;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Net;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Runtime.Serialization;
namespace SendSMS
{
[DataContract]
[KnownType(typeof(smsnew))]
public class smsnew
{
[Input("ComplaintNo")]
[DataMember]
public InArgument<string> ComplaintNo { get; set; }
[Input("SMS_No")]
[DataMember]
public InArgument<string> SMS_No { get; set; }
[Input("SMS_GATEWAY")]
[DataMember]
public InArgument<string> SMS_GATEWAY { get; set; }
}
public sealed class SMS : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
ITracingService tracingservice = context.GetExtension<ITracingService>();
try
{
IWorkflowContext workflowcontext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory servicefactory = context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = servicefactory.CreateOrganizationService(workflowcontext.UserId);
using (WebClient client = new WebClient())
{
var sms = new smsnew();
sms.ComplaintNo = sms.ComplaintNo;
sms.SMS_No = sms.SMS_No;
sms.SMS_GATEWAY = "501";
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(smsnew));
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, sms);

//var jsonObject = Encoding.Default.GetString(memoryStream.ToArray());
var webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
var serviceUrl = "http://10.101.51.10:8080/MessageEventManagement.ProcedureExecuterBatch/ProcedureExecuterBatch.svc";

// upload the data using Post mehtod
string response = webClient.UploadString(serviceUrl, memoryStream.ToString());
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}

}
}

*This post is locked for comments

  • Razim Khan Profile Picture
    Razim Khan 150 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    when i change it to text/xml the following error occured.

    The remote server returned an error: (400) Bad Request.

    my web service expect on parameter as json

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    Looks like you need an XML serializer as your remote server requires it. Maybe you need to check the WSDL of your remote server to know what schema is expected to use.

    And then I guess you need this to be like that:

    webClient.Headers[HttpRequestHeader.ContentType] = "'text/xml";

  • Razim Khan Profile Picture
    Razim Khan 150 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    infact WCF webservice accepting json parameter

  • Razim Khan Profile Picture
    Razim Khan 150 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    when i remove this line

    webClient.Headers[HttpRequestHeader.ContentType] = "application/json";

    the following error occoure

    The remote server returned an error: (415) Cannot process the message because the content type 'application/octet-stream' was not the expected type 'text/xml; charset=utf-8'.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    var webClient = new WebClient();
    webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
    var serviceUrl = "10.101.51.10/.../ProcedureExecuterBatch.svc";

    // upload the data using Post mehtod
    string response = webClient.UploadString(serviceUrl, memoryStream.ToString());

    >>>>

    web service is not accepting the json type. they are using typical XML format.

    go through the API and see what is the acceptable format/parameter you need to pass.

  • Razim Khan Profile Picture
    Razim Khan 150 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    Thank you @MёLvìN Fong,

    now the following error occured .

    The remote server returned an error: (415) Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'..

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    You need to restructure your code.

    1st you need to get your workflow class input and output argument correct. Don't include any additional weird code to mess up the piece of code.

    >input: complaint no

    >input: sms no

    >input: sms gateway

    >execute method:

    >var smslibrary = new smslibrary();

    >var response = smslibrary.yourmethod(complain no, sms no, sms gateway);

    2nd you can create a class file (we used to call it library) to perform the web service call.

    >class smslibrary

    >public response yourmethod(string complain no, string sms no, string sms gateway)

    >{

    bla bla bla...

    return webclient.uploadstring();

    >}

    reason being: you can plug your class file to a simple console program to do some unit test

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    Hi Razim Khan,

    Please refer the links below.

    stackoverflow.com/.../serialization-error-when-using-webservice-class-as-parameter-to-wcf

    social.msdn.microsoft.com/.../serialization-error-when-using-webservice-class-as-parameter-to-wcf

    Hope this helps you to solve this issue.

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    Hi Razim,

    sorry it is not quite correct.

    All the input attributes/properties needs to stay in the code activity class.

    You would only create the smsnew class with 3 string properties with same names.

    Then in the code activity you can instantiate smsnew class and set the string properties like this:

    smsnew sms1 = new smsnew();

    sms1.SMS_No = sms.SMS_No;

  • Razim Khan Profile Picture
    Razim Khan 150 on at
    RE: How to Resolve the following error , while calling a web service in dynamic crm workflow "Consider marking the base type 'System.Activities.CodeActivity' with DataContractAttribute or SerializableAttribute"

    thanks for your quick response, i have created new class like this,  

       [DataContract]

       [KnownType(typeof(smsnew))]

       public class smsnew

       {

           [Input("ComplaintNo")]

           [DataMember]

           public InArgument<string> ComplaintNo { get; set; }

           [Input("SMS_No")]

           [DataMember]

           public InArgument<string> SMS_No { get; set; }

           [Input("SMS_GATEWAY")]

           [DataMember]

           public InArgument<string> SMS_GATEWAY { get; set; }

       }

    rest of the code is correct ? ?
    i have update my code in question

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans