How can I test this method? [duplicate] - c#

I am using WCF Test Client (WcfTestClient.exe) for testing one of my wcf services.
I have a message contract which has a list of DataContracts as :
My message contract is as follows :
[MessageContract]
public class UpdateInvoiceStatusesRequest
{
private List<InvoiceStatusHistory> _invoiceStatusHistory;
[MessageBodyMember(Order = 0)]
public List<InvoiceStatusHistory> InvoiceStatusHistory
{
get { return _invoiceStatusHistory; }
set { _invoiceStatusHistory = value; }
}
}
and my data contract is :
[DataContract]
public class InvoiceStatusHistory
{
private int _invoiceId;
private int _status;
private string _comment;
private string _timeStamp;
[DataMember]
public int InvoiceId
{
get { return _invoiceId; }
set { _invoiceId = value; }
}
[DataMember]
public string Comment
{
get { return _comment; }
set { _comment= value; }
}
[DataMember]
public int Status
{
get { return _status; }
set { _status = value; }
}
[DataMember]
public string TimeStamp
{
get { return _timeStamp; }
set { _timeStamp = value; }
}
}
when i am using WcfTestClient.exe to test the service with UpdateInvoiceStatusesRequest message contract it shows the value of InvoiceStatusHistory as length = 0, now i don't know how can i add the objects of InvoiceStatusHistory in List<InvoiceStatusHistory> ?
Does anyone has any idea about it, please help me?

Type length=1 in the box. A + sign will appear next to the request parameter name. Click on it, then on the [0] node which indicates the first element in the array and set its values as usual.

Related

Assign string[] array field to another array field of same type c#

I have two classes defined in my solution
public class Registration {
[...]
public list<Account> Accounts {get; set;}
}
public class Account {
[...]
public string Code { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
In the web service that I am consuming, the following class definitions are available
public partial class VendReg {
[...]
private Payment_Details[] requestDetailsField;
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
public Payment_Details[] RequestDetails {
get {
return this.requestDetailsField;
}
set {
this.requestDetailsField = value;
}
}
}
public partial class Payment_Details {
private string bk_CodeField;
private string bk_NameField;
private string bk_AddressField;
public string Bk_Code {
get {
return this.bk_CodeField;
}
set {
this.bk_CodeField = value;
}
}
public string Bk_Name {
get {
return this.bk_NameField;
}
set {
this.bk_NameField = value;
}
}
public string Bk_Address {
get {
return this.bk_AddressField;
}
set {
this.bk_AddressField = value;
}
}
}
I want to assign Account to Request Details which is an array of Payment_Details. I tried this code below
vendReg.RequestDetails = registration.Accounts.Cast<Payment_Details>().ToArray();
I got invalid cast exception: Unable to cast object of type 'Account' to type 'Payment_Details'
Please guide on what I am not doing right
You need to convert this yourself (or you can look into things like Automapper)
vendReg.RequestDetails = registration.Accounts.Select(acc =>
new Payment_Details {
Bk_Code = acc.Code,
Bk_Name = acc.Name,
Bk_Address = acc.Address
}).ToArray();

Webservice does not serialize all properties of the request

Hei all,
I noticed a strange behavior of my SOAP Webservice.
When sending the request via soap ui i filled every property of the object.
While debugging the webservice only a few properties where filled.
It seems the top 5 properties, out of 15, are filled. When switching the properties in the Soap request, again the top 5 are filled.
Does anyone have a clue, why the serializing of the request object is not working ?
The interfaces are defined as OperationContract , properties and the request class as DataMember / DataContract.
Here is the Request Class
[DataContract]
public class Article
{
[DataMember]
public String ArticleName
{
get { return _ArticleName; }
set { _ArticleName = value; }
}
[DataMember]
public String Description
{
get { return _Description; }
set { _Description = value; }
}
[DataMember]
public Int ProductSubGroupNumber
{
get { return _ProductSubGroupNumber; }
set { _ProductSubGroupNumber = value; }
}
[DataMember]
public Double ArticleNumber
{
get { return _ArticleNumber; }
set { _ArticleNumber = value; }
}
[DataMember]
public Int Color
{
get { return _Color; }
set { _Color = value; }
}
[DataMember]
public Double Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
[DataMember]
public Int Version
{
get { return _Version; }
set { _Version = value; }
}
[DataMember]
public Int Material
{
get { return _Material; }
set { _Material = value; }
}
[DataMember]
public Int Warehouse
{
get { return _Warehouse; }
set { _Warehouse = value; }
}
}
And the Soap Request
<CreateArticle>
<ArticleGroup>1232456789</ArticleGroup>
<Articles>
<Article>
<ArticleName>test</ArticleName>
<Description>TEST 1213123</Description>
<ProductSubGroupNumber>987654</ProductSubGroupNumber>
<ArticleNumber>12345</ArticleNumber>
<Color>0</Color>
<Quantity>1</Quantity>
<Material>0</Material>
<Warehouse>0</Warehouse>
<Version>0</Version>
</Article>
</Articles>
</CreateArticle>
In the debugger are only Attributes till Color filled - the rest is Null.
If I swap them in the request, other are Filled and others not.
The Request Contains a ArticleGroupID as Int and an Array of Articles.
It looks like this is DataContractSerializer's fussy "order" enforcement. It really really wants to know what order the data will be in. You could try educating it:
[DataMember(Order=0)]
public string ArticleName {get;set;}
[DataMember(Order=1)]
public string Description {get;set;}
[DataMember(Order=2)]
public int ProductSubGroupNumber {get;set;}
[DataMember(Order=3)]
public double ArticleNumber {get;set;}
[DataMember(Order=4)]
public int Color {get;set;}
[DataMember(Order=5)]
public double Quantity {get;set;}
[DataMember(Order=8)]
public int Version {get;set;}
[DataMember(Order=6)]
public int Material {get;set;}
[DataMember(Order=7)]
public int Warehouse {get;set;}

How to use the Same Class on Client as on the Service?

How can I pass an entire defined class through a WCF service? I have the class defined on both the service and client side. I keep getting an error:
Best overloaded method match has some invalid arguments.
The whole class was copied from the client-side to the service-side.
Client side calling:
TransferProxy.PutTransferOnService(Transfer);
Defined on service:
[OperationContract]
bool PutTransferOnService(TypeTransfer Transfer);
I don't want to access individual items on the class from the client, I just want to move the WHOLE populated object through and do processing on the server side.
[DataContract]
public class TypeTransfer
{
private string userID;
private string transferNum;
private DateTime effectiveDate;
private int unitCount;
private int skuCount;
private string reason;
private string localStatus;
private string destStatus;
private string carrier;
private string sourceStore;
private string destinationStore;
private string inSeal;
private string outSeal;
[DataMember]
private List<TypeSOQ> correspondingSOQ = new List<TypeSOQ>();
[DataMember]
private List<TypeProductList> ProductList = new List<TypeProductList>();
public TypeTransfer() { }
// Function adds single item to transfer object
public void AddItem(int ProductID, string SKU, string PrimarySKU, string SCC, string ProductDescription, int TransferQty)
{
ProductList.Add(new TypeProductList
{
productID = ProductID,
sku = SKU,
primaryUPC = PrimarySKU,
scc = SCC,
description = ProductDescription,
transferQty = TransferQty
});
}
// Add SOQ to transfer object (can support multiple SOQ's)
public void AddSOQ(TypeSOQ soq)
{
correspondingSOQ.Add(soq);
}
// Function returns number of skus in Product List
public int GetSKUTotal()
{
return ProductList.Count();
}
// Function returns total number of items in transfer
public int GetItemTotal()
{
int itemtotal = 0;
for (int i = 0; i < ProductList.Count(); i++)
{
itemtotal += ProductList[i].transferQty;
}
return itemtotal;
}
// Return entire SOQ list
public List<TypeSOQ> GetSOQs()
{
return correspondingSOQ;
}
// Returns full product list in transfer object
public List<TypeProductList> GetProductList()
{
return ProductList;
}
[DataMember]
public string UserID
{
get { return userID; }
set { userID = value; }
}
[DataMember]
public string TransferNum
{
get { return transferNum; }
set { transferNum = value; }
}
[DataMember]
public DateTime EffectiveDate
{
get { return effectiveDate; }
set { effectiveDate = value; }
}
[DataMember]
public int UnitCount
{
get { return unitCount; }
set { unitCount = value; }
}
[DataMember]
public string Reason
{
get { return reason; }
set { reason = value; }
}
[DataMember]
public string LocalStatus
{
get { return localStatus; }
set { localStatus = value; }
}
[DataMember]
public string DestStatus
{
get { return destStatus; }
set { destStatus = value; }
}
[DataMember]
public string Carrier
{
get { return carrier; }
set { carrier = value; }
}
[DataMember]
public string SourceStore
{
get { return sourceStore; }
set { sourceStore = value; }
}
[DataMember]
public string DestStore
{
get { return destinationStore; }
set { destinationStore = value; }
}
[DataMember]
public string InSeal
{
get { return inSeal; }
set { inSeal = value; }
}
[DataMember]
public string OutSeal
{
get { return outSeal; }
set { outSeal = value; }
}
[DataMember]
public int SKUCount
{
get { return skuCount; }
set { skuCount = value; }
}
}
You said - The whole class was copied from the client-side to the service-side.
You don't need to copy your class to server side. just define your class in a separate library and give reference of that same library to both client and server.

inner classes and variables in WCF service class

I've got class in WCF. Inside this class - there are 2 classes, 1 interface and several variables. It adds as service reference without errors.
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class XmlService
{
private string filename;
private XmlTextReader xmlreader;
List<IWeather> returner=new List<IWeather>();
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
interface IWeather
{
string GetCondition { get; set; }
DateTime GetDate { get; set; }
}
public class Current_Weather:IWeather
{
private string condition, humidity, wind_condition;
private int temp_f, temp_c;
private DateTime day;
public string GetCondition
{
get { return condition; }
set { condition = value; }
}
public string GetHumidity
{
get { return humidity; }
set { humidity = value; }
}
public string GetWindCondition
{
get { return wind_condition; }
set { wind_condition = value; }
}
public int TEMP_F
{
get { return temp_f; }
set { temp_f = value; }
}
public int TEMP_C
{
get { return temp_c; }
set { temp_c = value; }
}
public DateTime GetDate
{
get { return day; }
set { day = value; }
}
}
public class Forecast_Weather:IWeather
{
public string condition;
public int lowT, highT;
public DateTime day;
public string GetCondition
{
get { return condition; }
set { condition = value; }
}
public int GetLowT
{
get { return lowT; }
set { lowT = value; }
}
public int HighT
{
get { return highT; }
set { highT = value; }
}
public DateTime GetDate
{
get { return day; }
set { day = value; }
}
}
}
Should I add contracts to variables,interface IWeather, for inner classes and its methods and variables?
If you want them serialzied and visible on the client they need to be marked with contract attributes.
Internal classes aren't really a good practice, instead put all your operations in one interface marked up as a service contract, then all your data contracts in their own class libraries so you can reference that assemply frmo your client. This facilitates writing your own proxies and other good habits.
Your internal classes will not be exposed to callers of your service, since your service doesn't use them either as parameters or as return values.

How to deserialize XML attribute using DataContract in Windows Phone 7

I have a XML document with a node like this.
<channel id="3102" platform = "1" activation="30/11/2010" desactivation="">
And I want to deserialize it using DataContract and Data Member attributes, which are working well with its properties but are not deserializing the attributes.
[DataContract(Namespace="")]
[XmlSerializerFormat]
public abstract class Channel
{
#region variables privadas
[DataContract(Namespace="")]
[XmlSerializerFormat]
//[KnownType(typeof(AudioChannel))]
//[KnownType(typeof(VideoChannel))]
public abstract class Channel
{
#region variables privadas
private DateTime _desactivation;
private DateTime _activation;
private int _platform;
private int _id;
....
#endregion
#region Propiedades públicas
[DataMember]
[XmlAttribute(AttributeName="desactivation")]
public DateTime Desactivation
{
get { return _desactivation; }
set { _desactivation = value; }
}
[DataMember]
[XmlAttribute(AttributeName="activation")]
public DateTime Activation
{
get { return _activation; }
set { _activation = value; }
}
[DataMember]
[XmlAttribute(AttributeName="platform")]
public int Platform
{
get { return _platform; }
set { _platform = value; }
}
#endregion
#region Propiedades públicas
[DataMember]
[XmlAttribute(AttributeName="desactivation")]
public DateTime Desactivation
{
get { return _desactivation; }
set { _desactivation = value; }
}
[DataMember]
[XmlAttribute(AttributeName="activation")]
public DateTime Activation
{
get { return _activation; }
set { _activation = value; }
}
[DataMember]
[XmlAttribute(AttributeName="platform")]
public int Platform
{
get { return _platform; }
set { _platform = value; }
} ...
My properties associated to these attributes are not filled, what I'm doing wrong?
Thanks in advance for any help provided.
Repeated question. How can you control .NET DataContract serialization so it uses XML attributes instead of elements? You can't do that with a DataContractSerializer, but you should achieve what you ask for using the XmlSerializer.

Categories