类的构造函数(从C#Web服务)将不会自动实现在C#中的MVC特性 [英] Class constructor (from C# web service) won't auto-implement properties in C# MVC
问题描述
我在我的本地机器上运行VS2010的两个实例。一个实例上运行我的Web服务(用C#编写)。另一个实例运行我的MVC的Web应用程序(也C#)。 MVC的Web应用程序对Web服务的引用。我可以成功地调用从MVC应用程序中的Web服务方法。
I'm running two instances of VS2010 on my local machine. One instance is running my Web Service (written in C#). The other instance is running my MVC web app (also C#). The MVC web app has a reference to the web service. I can successfully invoke web service methods from within the MVC app.
在我的Web服务是一个 PageNavigation
类:
In my web service is a PageNavigation
class:
// PageNavigation.cs
using System;
using System.Collections.Generic;
using System.Text;
public class PageNavigation
{
public string Page_Number { get; set; }
public string Page_Count { get; set; }
public PageNavigation()
{
Page_Number = "1";
Page_Count = "2";
}
}
在默认情况下,这个的应的返回与自动实现属性的对象时,我称之为类的构造函数:
By default, this should return an object with auto-implemented properties when I call the class constructor:
WebService.PageNavigation pageNavigation = new WebService.PageNavigation();
在Web服务在其他地方兴建PageNavigation对象时,这工作。
This works when constructing a PageNavigation object elsewhere in the web service.
pageNavigation.Page_Number
"1"
pageNavigation.Page_Count
"2"
但是,运行code的同一行中的MVC没有给同样的结果;对象的属性都是空值。
However, running the same line of code on the MVC isn't giving the same result; the object's properties are all null values.
pageNavigation.Page_Number
null
pageNavigation.Page_Count
null
这是预期的行为?有没有办法按预期来填充默认值的属性?如果需要了解更多信息,请让我知道,我会更新的问题。
Is this the expected behavior? Is there a way to populate the properties with default values as intended? If more information is needed please let me know and I will update the question.
推荐答案
该服务引用只看到你的对象,而不是业务逻辑的架构;你的情况,你的服务引用刚刚创建的MVC应用程序壳的数据类型。当你创建一个服务引用,它实际上创造的其他的类型,具有相同的属性名称和类型作为服务定义的类型。
The service reference only sees the schema of your object, not business logic; in your case, your service reference just created a shell data type in the MVC application. When you create a service reference, it's actually creating another type with the same property names and types as the type defined in the service.
有关您的特定方案(仅仅提供默认属性值,而不是更广泛的业务逻辑),你应该能够在 [System.ComponentModel.DefaultValue]
属性应用到您为了属性类发电机认识到,这些特性应以默认值进行填充。
For your particular scenario (simply providing default property values and not more general business logic), you should be able to apply the [System.ComponentModel.DefaultValue]
attribute to your properties in order for the class generator to recognize that these properties should be populated with a default value.
另外,当服务引用被重用现有的类型(如果你有这种类型的,是由服务和应用程序中引用这两个,例如公用库),那么你的业务逻辑将是完整的。
Incidentally, if the service reference were reusing existing types (if you had this type in a common library that was referenced both by the service and the application, for example), then your business logic would be intact.
另一种方法是,以实现一个工厂模式,由此在调用上实例化(和任选填充)的数据对象中的Web服务功能,则它返回到客户端。
An alternative would be to implement a factory pattern, whereby you call a function on the web service that instantiates (and optionally populates) the data object, then returns it to the client.
这篇关于类的构造函数(从C#Web服务)将不会自动实现在C#中的MVC特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!