Web service meta info
How can i send meta info to client, for example.
i have this method what is a web service (GetProductById) . My entity product have meta info with custom attributes.
i want send to my clients meta info . now how can i do like this or something similar?
Product GetProductById(int productId)
{
return IProductModel.GetProduct(ProductId);
}
Public Class Product
{
[Caption("Product id"]
int ProductId { get; private set; }
[Caption("Name of product")]
string Name { get; set ; 开发者_Python百科}
}
If you are using WCF then first you need to decorate your class with ServiceContract and each method to expose needs to be an OperationContract. Your data classes need to be DataContract and each member to expose [DataMember]
Take a look at WCFExtras on codeplex. It has a feature to include your source code xml comments in the WSDL generated. All you need to do is add a reference in your project and then add the attribute [XmlComments] to your class/interface of your Service Contract.
Your code would look like this:
[ServiceContract, XmlComments]
public class WebService
{
/// <summary>Returns the product information</summary>
[OperationContract]
Product GetProductById(int productId)
{
return IProductModel.GetProduct(ProductId);
}
}
/// <summary>Summary you want your client to see</summary>
[DataContract]
Public Class Product
{
/// <summary>Product id</summary
[DataMember(IsRequired = true)]
int ProductId { get; private set; }
/// <summary>Name of product</summary>
[DataMember(IsRequired = true)]
string Name { get; set ; }
}
This will ensure that the WSDL includes the comments you have added. Also make sure your generate Xml documentation file in your build config and have the file available at runtime.
精彩评论