Fixing WCF Deserialization Error : Invalid Message Body - c#
I'm setting up a new WCF service on a new server I got, but when I try to access the service through a xamarin application I get the following error
"Error in deserializing body of request message for operation 'GetString'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetString' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'GetStringAsync' and namespace 'http://tempuri.org/'"
I have used the method of creating a proxy (with a class library and connected service) for creating the client to be used in my xamarin application. I have used this exact method before without any problems, but now I can't seem to get this to work.
I have tried googling this, but the only similar errors I could find were where the methods name were the same, but the namespace as mentioned above was different which does not apply in my specific error. I have recreated the project, and redeployed a few times without any success.
IService
[ServiceContract]
public interface IPM_Service
{
[OperationContract]
List<dbUsersModel> GetUsers();
[OperationContract]
int InsertUser(dbUsersModel user, int userid);
[OperationContract]
string GetString(string test);
}
IService Implementation
public class PM_Service : IPM_Service
{
xamPropertyManagementEntities Entity = new xamPropertyManagementEntities();
public List<dbUsersModel> GetUsers()
{
var data = Entity.SP_SELECT_USERS().ToList();
List<dbUsersModel> users = new List<dbUsersModel>();
foreach (var item in data)
{
users.Add(new dbUsersModel() { dbUserID = item.USR_ID, dbUserName = item.USR_NAME, dbUserSurname = item.USR_SURNAME, dbUserUsername = item.USR_USERNAME, dbUserPassword = item.USR_PASSWORD, dbUserIDNo = item.USR_IDNO, dbUserContactNo = item.USR_CONTACTNO, dbUserEmail = item.USR_EMAIL, dbUserAddress1 = item.USR_ADDRESS1, dbUserAddress2 = item.USR_ADDRESS2, dbUserAddress3 = item.USR_ADDRESS3, dbUserAddress4 = item.USR_ADDRESS4 });
}
return users;
}
public int InsertUser(dbUsersModel user, int userid)
{
var insert = Entity.SP_INSERT_USER(user.dbUserUsername, user.dbUserPassword, user.dbUserName, user.dbUserSurname, user.dbUserIDNo, user.dbUserContactNo, user.dbUserEmail, user.dbUserAddress1, user.dbUserAddress2, user.dbUserAddress3, user.dbUserAddress4, userid);
decimal? returnID = insert.SingleOrDefault().Value;
int id = (int)returnID.DefaultIfEmpty(0);
return id;
}
public string GetString(string test)
{
return test;
}
}
public class dbUsersModel
{
public int dbUserID { get; set; }
public string dbUserUsername { get; set; }
public string dbUserPassword { get; set; }
public string dbUserName { get; set; }
public string dbUserSurname { get; set; }
public string dbUserIDNo { get; set; }
public string dbUserContactNo { get; set; }
public string dbUserEmail { get; set; }
public string dbUserAddress1 { get; set; }
public string dbUserAddress2 { get; set; }
public string dbUserAddress3 { get; set; }
public string dbUserAddress4 { get; set; }
}
Proxy Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// //
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace PMProxy
{
using System.Runtime.Serialization;
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.Runtime.Serialization.DataContractAttribute(Name="dbUsersModel", Namespace="http://schemas.datacontract.org/2004/07/PM_Service")]
public partial class dbUsersModel : object
{
private string dbUserAddress1Field;
private string dbUserAddress2Field;
private string dbUserAddress3Field;
private string dbUserAddress4Field;
private string dbUserContactNoField;
private string dbUserEmailField;
private int dbUserIDField;
private string dbUserIDNoField;
private string dbUserNameField;
private string dbUserPasswordField;
private string dbUserSurnameField;
private string dbUserUsernameField;
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserAddress1
{
get
{
return this.dbUserAddress1Field;
}
set
{
this.dbUserAddress1Field = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserAddress2
{
get
{
return this.dbUserAddress2Field;
}
set
{
this.dbUserAddress2Field = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserAddress3
{
get
{
return this.dbUserAddress3Field;
}
set
{
this.dbUserAddress3Field = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserAddress4
{
get
{
return this.dbUserAddress4Field;
}
set
{
this.dbUserAddress4Field = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserContactNo
{
get
{
return this.dbUserContactNoField;
}
set
{
this.dbUserContactNoField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserEmail
{
get
{
return this.dbUserEmailField;
}
set
{
this.dbUserEmailField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public int dbUserID
{
get
{
return this.dbUserIDField;
}
set
{
this.dbUserIDField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserIDNo
{
get
{
return this.dbUserIDNoField;
}
set
{
this.dbUserIDNoField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserName
{
get
{
return this.dbUserNameField;
}
set
{
this.dbUserNameField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserPassword
{
get
{
return this.dbUserPasswordField;
}
set
{
this.dbUserPasswordField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserSurname
{
get
{
return this.dbUserSurnameField;
}
set
{
this.dbUserSurnameField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public string dbUserUsername
{
get
{
return this.dbUserUsernameField;
}
set
{
this.dbUserUsernameField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="PMProxy.IPM_Service")]
public interface IPM_Service
{
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IPM_Service/GetUsers", ReplyAction="http://tempuri.org/IPM_Service/GetUsersResponse")]
System.Threading.Tasks.Task<PMProxy.dbUsersModel[]> GetUsersAsync();
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IPM_Service/InsertUser", ReplyAction="http://tempuri.org/IPM_Service/InsertUserResponse")]
System.Threading.Tasks.Task<int> InsertUserAsync(PMProxy.dbUsersModel user, int userid);
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IPM_Service/GetString", ReplyAction="http://tempuri.org/IPM_Service/GetStringResponse")]
System.Threading.Tasks.Task<string> GetStringAsync(string test);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
public interface IPM_ServiceChannel : PMProxy.IPM_Service, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
public partial class PM_ServiceClient : System.ServiceModel.ClientBase<PMProxy.IPM_Service>, PMProxy.IPM_Service
{
/// <summary>
/// Implement this partial method to configure the service endpoint.
/// </summary>
/// <param name="serviceEndpoint">The endpoint to configure</param>
/// <param name="clientCredentials">The client credentials</param>
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
public PM_ServiceClient() :
base(PM_ServiceClient.GetDefaultBinding(), PM_ServiceClient.GetDefaultEndpointAddress())
{
this.Endpoint.Name = EndpointConfiguration.BasicHttpBinding_IPM_Service.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public PM_ServiceClient(EndpointConfiguration endpointConfiguration) :
base(PM_ServiceClient.GetBindingForEndpoint(endpointConfiguration), PM_ServiceClient.GetEndpointAddress(endpointConfiguration))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public PM_ServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
base(PM_ServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public PM_ServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
base(PM_ServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public PM_ServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public System.Threading.Tasks.Task<PMProxy.dbUsersModel[]> GetUsersAsync()
{
return base.Channel.GetUsersAsync();
}
public System.Threading.Tasks.Task<int> InsertUserAsync(PMProxy.dbUsersModel user, int userid)
{
return base.Channel.InsertUserAsync(user, userid);
}
public System.Threading.Tasks.Task<string> GetStringAsync(string test)
{
return base.Channel.GetStringAsync(test);
}
public virtual System.Threading.Tasks.Task OpenAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IPM_Service))
{
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
return result;
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_IPM_Service))
{
return new System.ServiceModel.EndpointAddress("http://localhost:8733/Design_Time_Addresses/PM_Service/PM_Service/");
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}
private static System.ServiceModel.Channels.Binding GetDefaultBinding()
{
return PM_ServiceClient.GetBindingForEndpoint(EndpointConfiguration.BasicHttpBinding_IPM_Service);
}
private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
{
return PM_ServiceClient.GetEndpointAddress(EndpointConfiguration.BasicHttpBinding_IPM_Service);
}
public enum EndpointConfiguration
{
BasicHttpBinding_IPM_Service,
}
}
}
The only thing that I can possibly think of, is that my IIS may not be set up correctly, since this is the first time I have had to set up IIS by myself. The server is a VPS running Windows Server 2016. I may very well be wrong about the cause, and it may be something I have missed in the config of the service itself, or the proxy client I created.
Any ideas that might be able to help?
Related
Fill data to class
I have a c# class, I am creating object to put data in class , some data i have no able to do, here is my code InvoiceType oInvoiceType = new InvoiceType(); // my main object // using object UBLVersionIDType UBLVersionIDType oUBLVersionIDType = new UBLVersionIDType(); IdentifierType oIdentifierType = new IdentifierType(); oIdentifierType.Value = "UBL 2.1"; //oUBLVersionIDType.schemeID = oIdentifierType.schemeID; oUBLVersionIDType.Value = oIdentifierType.Value; // using object InvoiceTypeCodeType InvoiceTypeCodeType oInvoiceTypeCode = new InvoiceTypeCodeType(); CodeType oCodeType = new CodeType(); oCodeType.Value = "01"; oInvoiceTypeCode.Value = oCodeType.Value; // using Note NoteType oNoteType = new NoteType(); oNoteType.Value = new TextType oTextType = new TextType(); // this show error oTextType.Value = "This is a Note 1"; // this show error oNoteType.Value = oTextType.Value; // this show error // asign main object oInvoiceType.UBLVersionID = oUBLVersionIDType; oInvoiceType.InvoiceTypeCode = oInvoiceTypeCode; //oInvoiceType.Note = oNoteType; // this show error !!!!!!!! Next you can see the class public partial class InvoiceType { private UBLVersionIDType uBLVersionIDField; private InvoiceTypeCodeType invoiceTypeCodeField; private UBLExtensionType[] uBLExtensionsField; private NoteType[] noteField; public UBLVersionIDType UBLVersionID { get { return this.uBLVersionIDField; } set { this.uBLVersionIDField = value; } } public InvoiceTypeCodeType InvoiceTypeCode { get { return this.invoiceTypeCodeField; } set { this.invoiceTypeCodeField = value; } } public UBLExtensionType[] UBLExtensions { get { return this.uBLExtensionsField; } set { this.uBLExtensionsField = value; } } public NoteType[] Note { get { return this.noteField; } set { this.noteField = value; } } } public partial class NoteType : TextType1 { } public partial class TextType1 : TextType { } public partial class TextType { private string languageIDField; private string languageLocaleIDField; private string valueField; public string languageID { get { return this.languageIDField; } set { this.languageIDField = value; } } public string languageLocaleID { get { return this.languageLocaleIDField; } set { this.languageLocaleIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } public partial class UBLExtensionType { private IDType idField; private NameType1 nameField; private System.Xml.XmlElement extensionContentField; public IDType ID { get { return this.idField; } set { this.idField = value; } } public NameType1 Name { get { return this.nameField; } set { this.nameField = value; } } public System.Xml.XmlElement ExtensionContent { get { return this.extensionContentField; } set { this.extensionContentField = value; } } } The class "Invoice" has 4 fields to set data private UBLVersionIDType uBLVersionIDField; private InvoiceTypeCodeType invoiceTypeCodeField; private UBLExtensionType[] uBLExtensionsField; private NoteType[] noteField; As you can see in the code, I set data to "uBLVersionID" and "InvoiceTypeCode", but I have not been able to assign data to notefield, and I don't know to set "uBLExtensionfield". Does someone know how to do that?
Code below compiles with no errors. I added missing classes. The line with the ******* is causing issues. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { InvoiceType oInvoiceType = new InvoiceType(); // my main object // using object UBLVersionIDType UBLVersionIDType oUBLVersionIDType = new UBLVersionIDType(); IdentifierType oIdentifierType = new IdentifierType(); oIdentifierType.Value = "UBL 2.1"; //oUBLVersionIDType.schemeID = oIdentifierType.schemeID; oUBLVersionIDType.Value = oIdentifierType.Value; // using object InvoiceTypeCodeType InvoiceTypeCodeType oInvoiceTypeCode = new InvoiceTypeCodeType(); CodeType oCodeType = new CodeType(); oCodeType.Value = "01"; oInvoiceTypeCode.Value = oCodeType.Value; // using Note NoteType oNoteType = new NoteType(); //oNoteType.Value = new ******************************************** bad TextType oTextType = new TextType(); // this show error oTextType.Value = "This is a Note 1"; // this show error oNoteType.Value = oTextType.Value; // this show error // asign main object oInvoiceType.UBLVersionID = oUBLVersionIDType; oInvoiceType.InvoiceTypeCode = oInvoiceTypeCode; //oInvoiceType.Note = oNoteType; // this show error !!!!!!!! } } public class CodeType { public string Value { get; set; } } public class IdentifierType { public string Value { get;set;} } public class UBLVersionIDType { public string Value { get; set; } } public class InvoiceTypeCodeType { public string Value { get; set; } } public partial class InvoiceType { private UBLVersionIDType uBLVersionIDField; private InvoiceTypeCodeType invoiceTypeCodeField; private UBLExtensionType[] uBLExtensionsField; private NoteType[] noteField; public UBLVersionIDType UBLVersionID { get { return this.uBLVersionIDField; } set { this.uBLVersionIDField = value; } } public InvoiceTypeCodeType InvoiceTypeCode { get { return this.invoiceTypeCodeField; } set { this.invoiceTypeCodeField = value; } } public UBLExtensionType[] UBLExtensions { get { return this.uBLExtensionsField; } set { this.uBLExtensionsField = value; } } public NoteType[] Note { get { return this.noteField; } set { this.noteField = value; } } } public partial class NoteType : TextType1 { } public partial class TextType1 : TextType { } public partial class TextType { private string languageIDField; private string languageLocaleIDField; private string valueField; public string languageID { get { return this.languageIDField; } set { this.languageIDField = value; } } public string languageLocaleID { get { return this.languageLocaleIDField; } set { this.languageLocaleIDField = value; } } /// <remarks/> [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } public class IDType { } public class NameType1 { } public partial class UBLExtensionType { private IDType idField; private NameType1 nameField; private System.Xml.XmlElement extensionContentField; public IDType ID { get { return this.idField; } set { this.idField = value; } } public NameType1 Name { get { return this.nameField; } set { this.nameField = value; } } public System.Xml.XmlElement ExtensionContent { get { return this.extensionContentField; } set { this.extensionContentField = value; } } } }
I solved myself, This is the REAL answer and its working fine NoteType[] arrayNote = new NoteType[3]; NoteType oObj1 = new NoteType(); TextType oTextType = new TextType(); oTextType.Value = "This is Note 1"; oObj1.Value = oTextType.Value; arrayNote[0] = oObj1; oObj1.Value = "This is Note 2"; arrayNote[1] = oObj1;
XMLAttribute is not working
I am Deserializing my xml into C# class. <?xml version="1.0" encoding="UTF-8"?> <Applications> <Application Name = "name1" MakerCheckerType="Operational" SanctionCheckNeeded="N" PreExistCheckNeeded="N" > <InstrumentTypes> <InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity"> </InstrumentType> <InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name"> </InstrumentType> </InstrumentTypes> <ProcessSteps> <ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" /> <ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/> </ProcessSteps> </Application> <Application Name = "name2" MakerCheckerType="Real" SanctionCheckNeeded="Y" PreExistCheckNeeded="Y"> <InstrumentTypes> <InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity"> </InstrumentType> <InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name"> </InstrumentType> </InstrumentTypes> <ProcessSteps> <ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" /> <ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/> </ProcessSteps> </Application> </Applications> classes are: [XmlType("ProcessStep")] public class IMAProcessStep { private string name; private string vbaFunction; private string excelName; [XmlAttribute("Name")] public string Name { get { return name; } set { name = value; } } [XmlAttribute("VBAFunction")] public string VBAFunction { get { return vbaFunction; } set { vbaFunction = value; } } [XmlAttribute("ExcelName")] public string ExcelName { get { return excelName; } set { excelName = value; } } } [XmlType("InstrumentType")] public class IMAInstrumentType { [XmlAttribute("Name")] public string Name { get; set; } [XmlAttribute("Version")] public string Version { get; set; } [XmlAttribute("PrimaryKeyExcel")] public string PrimaryKeyExcel { get; set; } } [XmlType("Application")] public class IMAApplication { [XmlAttribute("Name")] public string Name { get; set; } [XmlAttribute("MakerCheckerType")] public string MakerCheckerType { get; set; } public bool IsMakerCheckerType { get { if (MakerCheckerType == "Real") return true; return false; } set { if (value) MakerCheckerType = "Real"; else MakerCheckerType = "Operational"; } } [XmlAttribute("SanctionCheckNeeded")] public string SanctionCheckNeeded { get; set; } [XmlAttribute("PreExistCheckNeeded")] public string PreExistCheckNeeded { get; set; } public bool IsSanctionCheckNeeded { get { return SanctionCheckNeeded == "Y"; } set { SanctionCheckNeeded = value ? "Y" : "N"; } } public bool IsPreExistCheckNeeded { get { if (PreExistCheckNeeded == "Y") return true; return false; } set { if (value) PreExistCheckNeeded = "Y"; else PreExistCheckNeeded = "N"; } } [XmlArray("ProcessSteps")] [XmlArrayItem(ElementName = "ProcessStep")] public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; } [XmlArray("InstrumentTypes")] [XmlArrayItem(ElementName = "InstrumentType")] public List<IMAProcessStep> ProcessSteps { get; set; } } Here How I am De serializing it... List<IMAApplication> appConfig = null; var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath); var xRoot = new XmlRootAttribute(); xRoot.ElementName = "Applications"; xRoot.IsNullable = true; var serializer = new XmlSerializer(typeof(List<IMAApplication>), xRoot); using (var stream = File.OpenRead(path)) appConfig = (List<IMAApplication>)serializer.Deserialize(stream); return appConfig; IMAApplication Deserialize successfully but processSteps and InstrumentTypes get only their Name attribute values. Rest of attributes are null. Can anyone tell me what wrong here.
Try following : using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; using System.Xml.Serialization; namespace ConsoleApplication1 { class Program { const string FILENAME = #"c:\temp\test.xml"; static void Main(string[] args) { IMAApplications applications = Deserialize(FILENAME); } static IMAApplications Deserialize(string configFilePath) { IMAApplications appConfig = null; var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath); var xRoot = new XmlRootAttribute(); xRoot.ElementName = "Applications"; xRoot.IsNullable = true; var serializer = new XmlSerializer(typeof(IMAApplications), xRoot); using (var stream = File.OpenRead(path)) appConfig = (IMAApplications)serializer.Deserialize(stream); return appConfig; } } [XmlRoot("ProcessStep")] public class IMAProcessStep { private string name; private string vbaFunction; private string excelName; [XmlAttribute("Name")] public string Name { get { return name; } set { name = value; } } [XmlAttribute("VBAFunction")] public string VBAFunction { get { return vbaFunction; } set { vbaFunction = value; } } [XmlAttribute("ExcelName")] public string ExcelName { get { return excelName; } set { excelName = value; } } } [XmlRoot("InstrumentType")] public class IMAInstrumentType { [XmlAttribute("Name")] public string Name { get; set; } [XmlAttribute("Version")] public string Version { get; set; } [XmlAttribute("PrimaryKeyExcel")] public string PrimaryKeyExcel { get; set; } } [XmlRoot("Applications")] public class IMAApplications { [XmlElement("Application")] public List<IMAApplication> applications { get; set; } } [XmlRoot("Application")] public class IMAApplication { [XmlAttribute("Name")] public string Name { get; set; } [XmlAttribute("MakerCheckerType")] public string MakerCheckerType { get; set; } public bool IsMakerCheckerType { get { if (MakerCheckerType == "Real") return true; return false; } set { if (value) MakerCheckerType = "Real"; else MakerCheckerType = "Operational"; } } [XmlAttribute("SanctionCheckNeeded")] public string SanctionCheckNeeded { get; set; } [XmlAttribute("PreExistCheckNeeded")] public string PreExistCheckNeeded { get; set; } public bool IsSanctionCheckNeeded { get { return SanctionCheckNeeded == "Y"; } set { SanctionCheckNeeded = value ? "Y" : "N"; } } public bool IsPreExistCheckNeeded { get { if (PreExistCheckNeeded == "Y") return true; return false; } set { if (value) PreExistCheckNeeded = "Y"; else PreExistCheckNeeded = "N"; } } [XmlArray("InstrumentTypes")] [XmlArrayItem(ElementName = "InstrumentType")] public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; } [XmlArray("ProcessSteps")] [XmlArrayItem(ElementName = "ProcessStep")] public List<IMAProcessStep> ProcessSteps { get; set; } } }
Marshal.GetComInterfaceForObject in c# returns E_INVALIDARG error message
I try to create a program that consumes plugins with COM-interface. I implement IClassFactory interface to create instances of plugin. The CreateInstance-Method is implemented this way: public int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject) { MessageBox.Show("TestClassFactory CreateInstance"); ppvObject = IntPtr.Zero; if (pUnkOuter != IntPtr.Zero) { // The pUnkOuter parameter was non-NULL and the object does // not support aggregation. Marshal.ThrowExceptionForHR(ComNative.CLASS_E_NOAGGREGATION); } if (riid == new Guid(ManagerGuids.ClassId) || riid == new Guid(ComNative.IID_IDispatch) || riid == new Guid(ComNative.IID_IUnknown)) { MessageBox.Show("Pre GetComInterfaceForObject"); // Create the instance of the .NET object // The container has to be passed as property and not by constructor injection. // This is because COM (de-)registration needs a constructor without parameters. ppvObject = Marshal.GetComInterfaceForObject(new NetPlugin(), typeof(INetPlugin)); MessageBox.Show("Post GetComInterfaceForObject"); } else { // The object that ppvObject points to does not support the // interface identified by riid. Marshal.ThrowExceptionForHR(ComNative.E_NOINTERFACE); } return WinError.S_OK; } The call of Marshal.GetComInterfaceForObject(new NetPlugin(), typeof(INetPlugin)); returns invalid argument error (E_INVALIDARG). The arguments of the method: NetPlugin: [ClassInterface(ClassInterfaceType.None)] [Guid(ManagerGuids.ClassId)] [ComVisible(true)] public class NetPlugin : INetPlugin { public NetPlugin() { } [ComVisible(true)] [DispIdAttribute(0x1)] public String LastParameters { get { return ""; } set { } } [ComVisible(true)] [DispIdAttribute(0x2)] public String Vendor { get { return "Company"; } set { } } [ComVisible(true)] [DispIdAttribute(0x3)] public String Description { get { return "Test plugin written in .NET"; } set { } } [ComVisible(true)] [DispIdAttribute(0x4)] public String Version { get { return "1.0"; } set { } } [ComVisible(true)] [DispIdAttribute(0x5)] public String Category { get { return "Void"; } set { } } [ComVisible(true)] [DispIdAttribute(0x6)] public String MenuEntry { get { return "NetPlugin"; } set { } } [ComVisible(true)] [DispIdAttribute(0x7)] public String MessageString { get { return "NetPlugin"; } set { } } [ComVisible(true)] [DispIdAttribute(0x8)] public String TooltipText { get { return "Tooltip NetPlugin"; } set { } } [ComVisible(true)] [DispIdAttribute(0x9)] public String FriendlyName { get { return "NetPlugin FFFFF Friendly"; } set { } } [ComVisible(true)] [DispIdAttribute(0xa)] public String BitmapResourceSmall { get { return "BitmapResourceSmall.bmp"; } set { } } [ComVisible(true)] [DispIdAttribute(0xb)] public String BitmapResourceLarge { get { return "BitmapResourceLarge.bmp"; } set { } } [ComVisible(true)] [DispIdAttribute(0xc)] public String Key { get { return ""; } // must be empty set { } } [ComVisible(true)] [DispIdAttribute(0xd)] public String Reserved { get { return "Void"; } set { } } [ComVisible(true)] [DispIdAttribute(0xe)] public Int32 Run() { //-- Return scode return 0; } } INetPlugin: [Guid(ManagerGuids.InterfaceId), ComVisible(true)] interface INetPlugin { [DispId(0x1)] String LastParameters { get; set; } [DispId(0x2)] String Vendor { get; set; } [DispId(0x3)] String Description {get; set; } [DispId(0x4)] String Version { get; set; } [DispId(0x5)] String Category { get; set; } [DispId(0x6)] String MenuEntry { get; set; } [DispId(0x7)] String MessageString { get; set; } [DispId(0x8)] String TooltipText { get; set; } [DispId(0x9)] String FriendlyName { get; set; } [DispId(0xa)] String BitmapResourceSmall { get; set; } [DispId(0xb)] String BitmapResourceLarge { get; set; } [DispId(0xc)] String Key { get; set; } [DispId(0xd)] String Reserved { get; set; } [DispId(0xe)] Int32 Run(); } The NetPlugin is registred this way: RegistrationServices rs = new RegistrationServices(); bool b = rs.RegisterAssembly(System.Reflection.Assembly.GetExecutingAssembly(), AssemblyRegistrationFlags.SetCodeBase); if (!b) return 998; String exePath = new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath; Registry.SetValue(#"HKEY_CLASSES_ROOT\CLSID\{" + ManagerGuids.ClassId + #"}\LocalServer32", "", exePath); The INetPlugin is registred this way: [HKEY_CLASSES_ROOT\Interface\InterfaceGUID] [HKEY_CLASSES_ROOT\Interface\InterfaceGUID\ProxyStubClsid32] #="{00020420-0000-0000-C000-000000000046}" [HKEY_CLASSES_ROOT\Interface\InterfaceGUID\TypeLib] #="{228a33a4-193a-4d6e-93ee-6aba49be9488}" "Version"="1.0" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface\InterfaceGUID] [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface\InterfaceGUID\ProxyStubClsid32] #="{00020420-0000-0000-C000-000000000046}" [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface\InterfaceGUID\TypeLib] #="{228a33a4-193a-4d6e-93ee-6aba49be9488}" "Version"="1.0" I'll be very thankful for any help!
WCF generate proxy classes with namespace as it is in server side
I have below class: namespace ESF.Engine.SharedObjects.CatalogManagement { [DataContract()] public class Customer { private string _LastName; private System.DateTime _BirthDate; private decimal _MoneyInCash; private bool _IsActive; private string _AdditionalInfo; private decimal _ITEMID; private string _NAME; [DataMember()] public string LastName { get { return this._LastName; } set { this._LastName = value; } } [DataMember()] public System.DateTime BirthDate { get { return this._BirthDate; } set { this._BirthDate = value; } } [DataMember()] public decimal MoneyInCash { get { return this._MoneyInCash; } set { this._MoneyInCash = value; } } [DataMember()] public bool IsActive { get { return this._IsActive; } set { this._IsActive = value; } } [DataMember()] public string AdditionalInfo { get { return this._AdditionalInfo; } set { this._AdditionalInfo = value; } } [DataMember()] public decimal ITEMID { get { return this._ITEMID; } set { this._ITEMID = value; } } [DataMember()] public string NAME { get { return this._NAME; } set { this._NAME = value; } } } } After adding Service Reference, the proxy for this class generated under namespace which is default for client application. Is it possible to force the class proxy to be generated as it is in server? Thanks for your time.
WCF ignore data contract not working
I have a WCF Service hosted on IIS. Here is my Interface: [ServiceContract] [SilverlightFaultBehavior] public interface IETC { [OperationContract] [PrincipalPermission(SecurityAction.Demand, Role = "XYZ")] string GetStampXML(); [OperationContract] [PrincipalPermission(SecurityAction.Demand, Role = "XYZ")] List<Stamp> GetStamps(); } I am getting an error when I go to my WCF service through the web browser. The error is as follows: Type 'System.Windows.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute..... My stamps Class is: [DataContract] public class Stamp { private string _Name; private string _SmallIcon = ""; private string _MediumIcon = ""; private string _LargeIcon = ""; private BitmapImage _SmallImage; private BitmapImage _MediumImage; private BitmapImage _LargeImage; [DataMember] public string Name { get { return _Name; } set { _Name = value; } } [DataMember] public string SmallIcon { get { return _SmallIcon; } set { _SmallIcon = value; } } [DataMember] public string MediumIcon { get { return _MediumIcon; } set { _MediumIcon = value; } } [DataMember] public string LargeIcon { get { return _LargeIcon; } set { _LargeIcon = value; } } [IgnoreDataMember] public BitmapImage SmallImage { get { return _SmallImage; } set { _SmallImage = value; } } [IgnoreDataMember] public BitmapImage MediumImage { get { return _MediumImage; } set { _MediumImage = value; } } [IgnoreDataMember] public BitmapImage LargeImage { get { return _LargeImage; } set { _LargeImage = value; } } } It is like the IgnoreDataMember is not being recognized. I tried it without the IgnoreDataMember figure it was going to only serialize the DataMembers, and that didn't work either. Any ideas why it seems to trying to serialize the BitmapImage?
What version of .net are you running? .NET 4 Data Contract does not require you to explicitly set Ignore attributes. You can test what's being produced by using DataContractSerializer and writing the content to the file. Create console application and reference your service project. namespace SO_10281928 { class Program { static void Main(string[] args) { var instance = new Stamp { Name = "Test", SmallIcon = "Small Icon", LargeIcon = "LargeIcon", MediumIcon = "MediumIcon" }; using (var stream = new FileStream(#"c:\temp\stamp.xml", FileMode.Create)) { var ds = new DataContractSerializer(typeof (Stamp)); ds.WriteObject(stream, instance); } Console.WriteLine("Done."); Console.ReadLine(); } } [DataContract] public class Stamp { private string _Name; private string _SmallIcon = ""; private string _MediumIcon = ""; private string _LargeIcon = ""; private BitmapImage _SmallImage; private BitmapImage _MediumImage; private BitmapImage _LargeImage; [DataMember] public string Name { get { return _Name; } set { _Name = value; } } [DataMember] public string SmallIcon { get { return _SmallIcon; } set { _SmallIcon = value; } } [DataMember] public string MediumIcon { get { return _MediumIcon; } set { _MediumIcon = value; } } [DataMember] public string LargeIcon { get { return _LargeIcon; } set { _LargeIcon = value; } } public BitmapImage SmallImage { get { return _SmallImage; } set { _SmallImage = value; } } public BitmapImage MediumImage { get { return _MediumImage; } set { _MediumImage = value; } } public BitmapImage LargeImage { get { return _LargeImage; } set { _LargeImage = value; } } } public class BitmapImage { } } And the result is : <Stamp xmlns="http://schemas.datacontract.org/2004/07/SO_10281928" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <LargeIcon>LargeIcon</LargeIcon> <MediumIcon>MediumIcon</MediumIcon> <Name>Test</Name> <SmallIcon>Small Icon</SmallIcon> </Stamp>