2012-04-23 83 views
2

我正在尝试编写一个C#宁静的Web服务,它在帐户编号作为参数传递时返回客户名称。将字符串返回到自定义对象类型

我有一个客户类:

public class Customer_T 
{ 
    public string CustName { get; set; } 
} 

接口类:

[ServiceContract(Namespace = "", Name = "CustomerInfoService")] 
public interface CustomerInfo_I 
{ 
    [OperationContract] 
    Customer_T CustName(string accountno); 
} 

*另一个叫CustomerInfo类,它实现CustomerInfo_I接口:*

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class CustomerInfo : CustomerInfo_I 
{ 
    [WebInvoke(Method = "GET", UriTemplate = "customer/{accountno}", ResponseFormat = WebMessageFormat.Json)] 
    public Customer_T CustName(string accountno) 
    { 
     string custName = ""; 
     CustNameByAccountNo custdb = new CustNameByAccountNo(); 
     custName = custdb.getCustName(accountno).ToString(); 
     if (custName.Equals("") == false) 
     { 
      return new Customer_T { CustName = custName }; 
     } 
     else 
     { 
      return null; //This is where I want to change 
     } 
    } 
} 

而不是返回null,我想返回一个字符串“InvalidAccNo”
我尝试过,但它给了我一个错误。

Cannot implicitly convert type 'string' to 'CustomerInfo.Service.Customer_T' 
+0

你为什么要返回Customer_T是你想返回一个字符串? – SimpleVar 2012-04-23 07:32:37

+0

Customer_T具有在该类中定义的CustName属性。如果没有客户名称被返回,我想显示字符串“InvalidCustAcc”。我不希望将字符串分配给CustName属性。 – cookiemonsta 2012-04-23 09:35:45

回答

0

由于您的返回类型定义为Customer_T,您将无法返回字符串。您可能需要考虑的一个选项是查看WCF内部的错误处理,以向用户返回错误,而不是重新调整单个对象。抛出错误可用于指示指定了无效的帐号。

[ServiceContract(Namespace = "", Name = "CustomerInfoService")] 
public interface CustomerInfo_I 
{ 
    [OperationContract] 
    [FaultContract(typeof(string))] 
    Customer_T CustName(string accountno); 
} 

http://msdn.microsoft.com/en-us/library/ms733721.aspx

此外,您的客户类,你应该标记与DataContract属性及与DataMember数据成员的类,以便返回复杂类型。

[DataContract] 
public class Customer_T 
{ 
    [DataMember] 
    public string CustName { get; set; } 
} 
0

这是你想要的吗?

if (custName.Equals("") == false) 
    { 
     return new Customer_T { CustName = custName }; 
    } 
    else 
    { 
     return new Customer_T { CustName = "Invalid Customer Name!" }; 
    } 

虽然听起来好像你可能想在Customer_T的IsValid属性或类似的东西。 Customer_T它设置为false在第二种情况,并重写的ToString:

public override string ToString() 
{ 
    return this.IsValid ? this.CusName : "Invalid Customer Name!"; 
} 

然后,你可以这样做:

if (custName.Equals("") == false) 
    { 
     return new Customer_T { CustName = custName }; 
    } 
    else 
    { 
     return new Customer_T { IsValid = false }; 
    } 

只要确保你在Customer_T构造设置IsValid的为真,那么它默认是有效的。

+0

这比以前工作得更好。而不是返回null,它现在返回null给CustName属性。我想要做的是显示一个字符串“InvalidCustAcc”,而不是显示CustName属性。我希望我在这里很清楚。谢谢:) – cookiemonsta 2012-04-23 09:29:32

0

该方法声明为返回Customer_T,该字符串不是实例。我会建议反对它,但如果你真的想要一个方法产生一个对象或一个字符串的实例,你可以让它返回object。其中Customer_Tstring都是。

变化

public Customer_T CustName(string accountno) 

分为:

public object CustName(string accountno) 

这样做的问题是,现在该方法的调用者并不知道肯定会造成什么。

+0

刚刚试过这个..改变类型* Customer_T *到*对象*没有工作。它现在给我一个** HTTP/1.1 400错误请求** – cookiemonsta 2012-04-23 09:14:36

+0

可能你的web服务的使用者期望找到一个产生'Customer_T'的方法,而这种方法现在不再存在。 – Bazzz 2012-04-23 09:46:26

+0

那么有没有什么可能的方法来做这里所要求的? – cookiemonsta 2012-04-23 10:06:29

0

编译器是正确的。 在你的接口中,你已经声明CustName返回一个Customer_T,但是现在你已经改变了主意并需要一个字符串作为返回值。
从web服务合同的角度来看,这并不好。

因此,如果您需要Customer_T作为返回值,请不要更改任何内容,并在未找到custname时引发异常。替代方案是

  • 添加一个新的方法与适当的签名
  • 变化(重大更改)你的方式,返回的CustName一个 字符串合同。