c# - ServiceKnownType client side issue -
server side:
service details:
[serviceknowntype("getknowntypes", typeof(helper))] [servicecontract] public interface iappconfigurationmanager { [operationcontract] object changeobjectproperty(object mycustomobject); }
implementation:
public class appconfigurationmanager : iappconfigurationmanager { public object changeobjectproperty(object mycustomobject) { foreach (type type in globals.mycustomtypes) { if (type == mycustomobject.gettype()) { foreach (propertyinfo property in mycustomobject.gettype().getproperties()) { if(property.name == "id") { int oldvalue = convert.toint32(property.getvalue(mycustomobject)); property.setvalue(mycustomobject, oldvalue + 1); break; }; }; } } return mycustomobject; } }
helper class used collecting of knowntypes:
public static class helper { public static ienumerable<type> getknowntypes(icustomattributeprovider provider) { globals.mycustomtypes = new list<type>(); system.collections.generic.list<system.type> knowntypes = new system.collections.generic.list<system.type>(); assembly assembly = assembly.loadfrom(@"d:\wcf\runtimemanagement\bin\debug\runtimemanagement.dll"); foreach (type type in assembly.gettypes()) { if (type.name.contains("customer") || type.name.contains("account")) { knowntypes.add(type); globals.mycustomtypes.add(type); }; }; return knowntypes; } }
classes:
[datacontract] public class customer { [datamember] public int id { get; set; } [datamember] public string name { get; set; } } [datacontract] public class account { [datamember] public int id { get; set; } [datamember] public string name { get; set; } [datamember] public customer accountowner { get; set; } }
client side:
before invoking service, trying register knowntypes @ client side also:
private void fmmain_load(object sender, eventargs e) { mycustomtypes = new list<type>(); assembly assembly = assembly.loadfrom(@"d:\wcf\runtimemanagement\bin\debug\runtimemanagement.dll"); foreach (type type in assembly.gettypes()) { if (type.name.contains("customer") || type.name.contains("account")) { mycustomtypes.add(type); if (type.name == "customer") { customertype = type; }; foreach (var operation in globals.appconfigmgrclient.endpoint.contract.operations) { operation.knowntypes.add(type); }; }; }; object mycustomerobject = activator.createinstance(customertype); foreach (propertyinfo property in mycustomerobject.gettype().getproperties()) { if (property.name == "id") { property.setvalue(mycustomerobject, 111); break; }; }; mycustomerobject = globals.appconfigmgrclient.changeobjectproperty(mycustomerobject); foreach (propertyinfo property in mycustomerobject.gettype().getproperties()) { if (property.name == "id") { debug.writeline(property.getvalue(mycustomerobject).tostring()); break; }; }; }
got error here:
mycustomerobject = globals.appconfigmgrclient.changeobjectproperty((runtimemanagement.catalogmanagement.customer)mycustomerobject);
the formatter threw exception while trying deserialize message: there error while trying deserialize parameter http://tempuri.org/:mycustomobject. innerexception message 'element 'http://tempuri.org/:mycustomobject' contains data type maps name 'http://tempuri.org/:customer'. deserializer has no knowledge of type maps name. consider using datacontractresolver or add type corresponding 'customer' list of known types - example, using knowntypeattribute attribute or adding list of known types passed datacontractserializer.'. please see innerexception more details.
may long post, hope got idea trying here:
- adding
customer
,account
classesserviceknowntype
@ server side helper class. - on load event of client side, trying register
knowntypes
same assemblycustomer
,account
exists. - then creating instance of
customer
type, set it'sid
property 111, sendoperationcontract
calledchangeobjectproperty
. after successful execution, should increment id.
thanks time.
Comments
Post a Comment