2

I have encountered a strange issue. When I update one of my websites to a web service by

  1. Updating it or
  2. Removal of the service and readding it altogether

The intellisense still reports that the type, in this case Document_Type3 is still the same type of an object generated by the service. I can set my object type to `Document_Type3'. I build the project, no issues.

enter image description here

However, when I run the project, I get a compiler error saying that that my object which is Document_Type3 doesn't contain the Order.

Compiler Error Message: CS0117: 'DynamicsNAV.Document_Type3' does not contain a definition for 'Order'

Source Error:

Line 376:                comment.Date = DateTime.Now;
Line 377:                comment.DateSpecified = true;
Line 378:                comment.Document_Type = Document_Type3.Order;  <-- right here.
Line 379:                comment.Document_TypeSpecified = true;
Line 380:                comment.Line_No = i * 1000;

The hell it doesn't. I can see it right there.

  <xsd:simpleType name="Document_Type">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Quote" />
      <xsd:enumeration value="Order" />
    </xsd:restriction>
  </xsd:simpleType>

I can set it fine, compile it fine -- but I can't run it.

I nuked my temporary files in

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

thinking that it perhaps has cached these dynamically generated objects. I rebuilt the project and watched the website reappear, along with the objects, in the temporary directory.

When I run it -- same thing -- no compiler error until it actually loads in the browser. An important issue to note is that these services are being returned by Dynamics NAV, and types that have the same name, such as Document_Type, will be appended with a number on the end. In the code, again, Document_Type3 contains Order and Quote.

What the heck is going on?

4

1 回答 1

1

这绝对是 Visual Studio/Web 服务代码生成中的一个错误。 这是我对我认为正在发生的事情以及如何解决它的解释。

在存在非明确命名的对象类型的 NAV(或任何其他 Web 服务)中,在这种情况下"Document_Type",显然构建在编译时出现故障。例如,我可能有三个单独的 WSDL 对象,它们都定义了一个文档类型。例如

CustomerSale.wsdl
WebSale.wsdl
VendorSale.wsdl

当 Visual Studio 代码生成对象时,它会遍历我的每个 WSDL 对象,找到 Document_Type,并进行相应的枚举。例如

CustomerSale.Document_Type -> Document_Type1
WebSale.Document_Type -> Document_Type2
VendorSale.Document_Type -> Document_Type3

但是,当 Web 编译对象时,对象会出现乱序。例如

CustomerSale.Document_Type -> Document_Type1
WebSale.Document_Type -> Document_Type3
VendorSale.Document_Type -> Document_Type2

我不确定这是否取决于对象的字母顺序或其他变量,但它肯定会发生。

我看不出它们会失去同步的韵律或原因,但简单的解决方案(在我的情况下)是简单地删除 WSDL 对象,从而导致编译器混淆。反正我也不需要。

为了在运行时查找导致编译器混淆的对象,我反映了类型:

Response.Write(WebSale.Document_Type.GetType().ToString);
Response.End();

然后我转到定义,在本例中为 Document_Type2,发现它绝对不是同一个对象。然后我从我的 Web 服务和 web.config 中删除了该对象——然后我重新编译。我的 Web 对象立即变成了 Document_Type2!这清除了所有内容并允许站点在实际浏览器中编译。他们现在同步了。

但是,我知道并非每个人都有可以删除的对象,因为他们实际上可能正在使用这些对象。另一种解决方法是通过反射动态设置对象。除了将您的对象类型绑定到“Doc​​ument_Type2”之外,您还可以根据需要反映类型并进行强制转换。例如

//Document type will throw error at run time.
//comment.Document_Type = DynamicsNAV.Document_Type3.Order; <-- removed.
// I don't care what the name of the object is,
// I know that it contains Order.   
var prop = comment.GetType().GetProperty("Document_Type");
prop.SetValue(comment, Enum.Parse(prop.PropertyType, "Order"), null);

我希望这个解决方案可以帮助遇到同样问题的其他人!

于 2011-05-12T15:43:27.863 回答