Problem referencing class library in a web service
-
Hi, I have a simple class library (Common.dll) that contains one public enum public enum Product { Deliver = 0, Order, MSM } I have a web service that uses Common.dll
[WebMethod]
public string GetProductDescription(Common.Product product) {
if(product == Common.Product.Deliver)
{
return "Deliver";
}
else if(product == Common.Product.Order){
return "Order";
}
else if(product == Common.Product.MSM){
return "MSM";
}
return string.Empty;
}The web service compiles without problems. I have a simple windows forms application that has a reference to Common.dll and to the web service I have a method which calls the web service GetProductDescription
private string GetProductDescription(Common.Product product)
{
return ws.GetProductDescription(product);
}I get a compiler error saying "cannot convert from 'Common.Product' to 'WindowsApplication1.localhost.Product'" I looked at the generated code for the web reference (reference.cs) and the Product enum has been defined so it seems that it is completely ignoring the enum from Common.dll
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.itutopia.net/WebSync3")]
public enum DataBaseAction {/// Insert, /// Update, /// Delete,
}
I'm pretty stumped as to what's causing this to happen so any advice or suggestions will be well appreciated. Thanks, dlarkin77
-
Hi, I have a simple class library (Common.dll) that contains one public enum public enum Product { Deliver = 0, Order, MSM } I have a web service that uses Common.dll
[WebMethod]
public string GetProductDescription(Common.Product product) {
if(product == Common.Product.Deliver)
{
return "Deliver";
}
else if(product == Common.Product.Order){
return "Order";
}
else if(product == Common.Product.MSM){
return "MSM";
}
return string.Empty;
}The web service compiles without problems. I have a simple windows forms application that has a reference to Common.dll and to the web service I have a method which calls the web service GetProductDescription
private string GetProductDescription(Common.Product product)
{
return ws.GetProductDescription(product);
}I get a compiler error saying "cannot convert from 'Common.Product' to 'WindowsApplication1.localhost.Product'" I looked at the generated code for the web reference (reference.cs) and the Product enum has been defined so it seems that it is completely ignoring the enum from Common.dll
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.itutopia.net/WebSync3")]
public enum DataBaseAction {/// Insert, /// Update, /// Delete,
}
I'm pretty stumped as to what's causing this to happen so any advice or suggestions will be well appreciated. Thanks, dlarkin77
What's your definition for
WindowsApplication1.localhost.Product
? How did you bugger up your formatting? It looks like you tried to do it right, but something is getting the way."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
What's your definition for
WindowsApplication1.localhost.Product
? How did you bugger up your formatting? It looks like you tried to do it right, but something is getting the way."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Hi, I accidentally posted the definition for WindowsApplication1.localhost.DatabaseAction instead of the one for Product. It's defined as follows:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.itutopia.net/WebSync3")]
public enum Product {/// Deliver, /// Order, /// MSM, }
The formatting thing is a bit weird. I wrapped the code segment in a code block and checked the 'Ignore HTML tags' option. Unless I'm mistaken that's all I need to do to format it correctly. Is there some extra step I need to make? Thanks, dlarkin77
-
Hi, I accidentally posted the definition for WindowsApplication1.localhost.DatabaseAction instead of the one for Product. It's defined as follows:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.itutopia.net/WebSync3")]
public enum Product {/// Deliver, /// Order, /// MSM, }
The formatting thing is a bit weird. I wrapped the code segment in a code block and checked the 'Ignore HTML tags' option. Unless I'm mistaken that's all I need to do to format it correctly. Is there some extra step I need to make? Thanks, dlarkin77
As to your problem, the only thing I can think of is that you may have to convert the enum to an int, return the int to the calling application, and let the calling app convert it back to a Product enum. Here's a function I use to go back to an enum from an integer value. It ensures that enum "value" exists within the specified enum type, thereby never returning an invalid enum value.
//--------------------------------------------------------------------------------
/// <summary>
/// Casts the specified integer to an appropriate enumerated value of the specified
/// enum type. If all else fails, the enum will be returned as the specified default
/// ordinal.
/// </summary>
/// <param name="value">The integer value representing an enumeration element
/// <param name="deafaultValue">The default enum value to be used if the specified
/// "value" does not exist in the enumeration definition
/// <returns>The appropriate enum of the specified enum type</returns>
public static T IntToEnum(int value, T defaultValue)
{
T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
return enumValue;
}The formatting thing is a site bug.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
As to your problem, the only thing I can think of is that you may have to convert the enum to an int, return the int to the calling application, and let the calling app convert it back to a Product enum. Here's a function I use to go back to an enum from an integer value. It ensures that enum "value" exists within the specified enum type, thereby never returning an invalid enum value.
//--------------------------------------------------------------------------------
/// <summary>
/// Casts the specified integer to an appropriate enumerated value of the specified
/// enum type. If all else fails, the enum will be returned as the specified default
/// ordinal.
/// </summary>
/// <param name="value">The integer value representing an enumeration element
/// <param name="deafaultValue">The default enum value to be used if the specified
/// "value" does not exist in the enumeration definition
/// <returns>The appropriate enum of the specified enum type</returns>
public static T IntToEnum(int value, T defaultValue)
{
T enumValue = (Enum.IsDefined(typeof(T), value)) ? (T)(object)value : defaultValue;
return enumValue;
}The formatting thing is a site bug.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Hi, I figured I could probably do something like what you've suggested but I was hoping that I wouldn't have to go down that road. Is there any particular reason that the enum is being redefined in the generated stub for the web service? Why doesn't the web service use the definition in the class library that it references? Thanks, dlarkin77
-
Hi, I figured I could probably do something like what you've suggested but I was hoping that I wouldn't have to go down that road. Is there any particular reason that the enum is being redefined in the generated stub for the web service? Why doesn't the web service use the definition in the class library that it references? Thanks, dlarkin77
I think the problem is that since the web service is a different binary, the calling application doesn't know what to do with the value. I make it a habit to only return intrinsic types and DataTables from web services. I convert enums to int before returning them. Have you looked at the returned value in the debugger? You might have gotten lucky and it might already be an
int
. John's 1st rule of programming: If there are several ways to accomplish the same task, the one that creates the most effort on the part of the programmer is probably to only way to do it right."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
I think the problem is that since the web service is a different binary, the calling application doesn't know what to do with the value. I make it a habit to only return intrinsic types and DataTables from web services. I convert enums to int before returning them. Have you looked at the returned value in the debugger? You might have gotten lucky and it might already be an
int
. John's 1st rule of programming: If there are several ways to accomplish the same task, the one that creates the most effort on the part of the programmer is probably to only way to do it right."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi, I have a simple class library (Common.dll) that contains one public enum public enum Product { Deliver = 0, Order, MSM } I have a web service that uses Common.dll
[WebMethod]
public string GetProductDescription(Common.Product product) {
if(product == Common.Product.Deliver)
{
return "Deliver";
}
else if(product == Common.Product.Order){
return "Order";
}
else if(product == Common.Product.MSM){
return "MSM";
}
return string.Empty;
}The web service compiles without problems. I have a simple windows forms application that has a reference to Common.dll and to the web service I have a method which calls the web service GetProductDescription
private string GetProductDescription(Common.Product product)
{
return ws.GetProductDescription(product);
}I get a compiler error saying "cannot convert from 'Common.Product' to 'WindowsApplication1.localhost.Product'" I looked at the generated code for the web reference (reference.cs) and the Product enum has been defined so it seems that it is completely ignoring the enum from Common.dll
///
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3053")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.itutopia.net/WebSync3")]
public enum DataBaseAction {/// Insert, /// Update, /// Delete,
}
I'm pretty stumped as to what's causing this to happen so any advice or suggestions will be well appreciated. Thanks, dlarkin77
The problem is that even though you have the same assembly (common.dll) on both sides, the classes are different. When WebService proxy is created at client side, Visual Studio creates new classes for all classes returned by WebService (based on WSDL). I solved this by not using Visual Studio Web References. Instead I use WSDL.exe on client side and create the proxy class manually. After the creation is done I strip away all the class definitions that are created for parameters and convert the parameters used in web service calls to classes from common assembly. Then I include this file to the project. For more info on WSDL, go to Web Services Description Language Tool (Wsdl.exe)[^] Hope this helps, Mika