How to pass an enum value to wcf webservice

纵然是瞬间 提交于 2020-01-10 19:32:49

问题


can ksoap2 pass an enum to webservice?

there is a wcf webservice:

[OperationContract]
string TestEnum(CodeType code);

CodeType is dotnet enum:

    public enum CodeType
    {
        [EnumMember]
        ALL,

        [EnumMember]
        VehicleColor
    }

How can i call this wcf webservice at android client?

i create a enum CodeType and implements KvmSerializable. In method getPropertyInfo, what is the value of info.name(info.type)?

public enum CodeType implements KvmSerializable, BaseInterface {
    ALL,

    VehicleColor;
//.......
    @Override
    public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
        //info.namespace = this.NameSpace;
        info.name = ?;
        info.type = ?;

    }
}

Thanks for your help.


回答1:


I just solved that enum-Problem via Marshal.

I created a Java-Enum "copying" the .net one. I then wrote a Marshal-Class for it:

public class MarshalEnum implements org.ksoap2.serialization.Marshal
{
    ... // Singleton-Pattern

     public Object readInstance(XmlPullParser xpp, String string, String string1,
                           PropertyInfo pi)
        throws IOException, XmlPullParserException
{
    return MyEnum.valueOf( xpp.nextText() );
}

public void writeInstance(XmlSerializer xs, Object o)
        throws IOException
{
    xs.text(((MyEnum)o).name());
}

public void register(SoapSerializationEnvelope sse)
{
    sse.addMapping(sse.xsd, "MyEnum", MyEnum.class, MarshalEnum.getInstance() );
}
} // class

Then, when calling the Method where MyEnum-Values shall be sent:

//... blah blah
SoapSerializationEnvelope envelope =
                          new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(SOAP_REMOTE_NAMESPACE, "MyEnum", MyEnum.class,       
                    MarshalEnum.getInstance());
//... and so on.

Note that SOAP_REMOTE_NAMESPACE is the data contract namespace of the enum to be used! See the wsdl-file to find it out if you're not sure. Should look something like "http://schemas.datacontract.org/2009/08/Your.dotNet.Namespace".

I hope this is going to work for you, too.




回答2:


Have you got

[ServiceContract]
[ServiceKnownType(typeof(CodeType))]
public interface ITheService
{
    [OperationContract]
    string TestEnum(CodeType code);
}

and

[DataContract]
public enum CodeType 
{
    // ...
}

?

Edit:

A bit of searching also turned up this, which may be of use...



来源:https://stackoverflow.com/questions/5827631/how-to-pass-an-enum-value-to-wcf-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!