Passing collection via asmx
I have a list of pairs (CarType(Enum), code(string)). Unfortunately in this collection codes are not unique.
Van,C1
Van,C2
Pickup,C1
How to pass this 开发者_开发知识库collection via asmx webservice.
Now I have KeyValuePair[] as parametr in webMethod
[webmethod]
public void DestroyCars(KeyValuePair<string, CarType>[] cars)
{
//implementation.
}
But when I update my webreference(not servicereference) and put instance of KeyValuePair[] in
service.DestroyCars(someinstance of KeyValuePair<string, CarType>[])
I get exception, because I must there KeyPairValuOfStringCarType.
Why is that and how to fix it ?
To be more clear:
I want to pass multidimension collection through webservice.
I create
[webmethod] public void DestroyCars(KeyValuePair[] cars) { //implementation. }
I update webreference of my application.
When I want to call this webmethod from my application with new instance of KeyValuePair[] I get error that this parameter is not assignable to parameter type of my webmethod.
Intellisense tells me that type of this parameter is KeyPairValuOfStringCarType with is not a true.
I check type of this type in net.reflector and I see that is a string.
I know there is no sense. This is way I ask for help :/
The reason you're encountering this error:
"parameter type "System.Collections.Generic.KeyValuePair[]" is not assignable to parameter type mywebservicenamespace.KeyPairValuOfStringCarType
Is because when asp.net creates the web-service proxy (the web service reference) for you, it "duplicates" any custom types that your web service exposes, in this instance KeyValuePair<string, CarType>
gets converted to KeyValuePairOfStringCarType
in the proxy, so that there's a way for you to pass these into the web service.
What you need to do to call your webservice is (this worked for me, in Visual Studio 2008, targeting an asmx
web service):
// Create a new CarKeyValuePair in your web service project
public struct CarKeyValuePair
{
public string Key { get; set; }
public CarType CarType { get; set; }
}
// Change your web method to use this instead of KeyValuePair
[WebMethod]
public void DestroyCars(CarKeyValuePair[] cars)
{
// Implementation here
}
You can then update your web service reference and write something similar to the following in the code that calls your web service:
service.DestroyCars(new CarKeyValuePair[] { new CarKeyValuePair() { CarType = CarType.Pickup, Key = "C1" } });
// or
var cars = new List<CarKeyValuePair>();
var car = new CarKeyValuePair();
car.CarType = CarType.Pickup;
car.Key = "C1";
cars.Add(car);
service.DestroyCars(cars.ToArray());
精彩评论