Reputation: 1720
I'm not even sure where to look for answers to my question as you can see from my very generic title.
What I'd like to do:
I have a WCF web service that pulls data from the database, say it's name is svcService. I'll use it in another program like this:
IsvcService data = new IsvcService ();
I'd like to be able to pull a variety of customer information from this service by only passing a customer ID and the way I would like to access the data would be like this:
data.Customer("MYCUST").Name (returns customer's name)
data.Customer("MYCUST").Address.City (returns customer's city)
data.Customer("MyCUST").Contact.EmailAddress (returns customer's email address)
What I tried:
I'm still fairly new at C# so I wasn't even quite sure where to look, but I thought maybe I could accomplish this by using Enums
and extending them, or by using different classes.
The end result I'm looking for is similar to this question and this question, but then how do I pass the customer ID into my webservice in the way I showed above?
How would I structure my code inside my web service? Is what I'm asking even possible?
P.S. Feel free to edit the title and tags to better describe what I'm asking.
Upvotes: 1
Views: 153
Reputation: 28530
I would suggest a slightly different approach. Use a class that contains the customer information and decorate it with the [DataContract]
attribute. You can return the customer object to your client, and the client can then access the properties in the object to get the relevant data.
For example, you could have a class like this:
[DataContract(Namespace = "http://namespacename/")]
public class Customer
{
[DataMember]
public string Name { get; set; }
[DataMember]
public CustomerAddress { get; set; }
[DataMember]
public CustomerContact { get; set; }
// Other properties as desired/needed\
}
A couple of things to point out. First, it looks like most any other class (just one with nothing but properties), but the class is decorated with the [DataContract]
attribute and each property is decorated with the [DataMember]
attribute. Only properties that are decorated with the [DataMember]
attribute will be serialized by the DataContractSerializer.
Secondly, you can have nested classes in a Data Contract - note the CustomerAddress
and CustomerContact
properties. Both of these would be Data Contracts as well.
For example:
[DataContract(Namespace = "http://namespacename/")]
public class CustomerAddress
{
[DataMember]
public string City { get; set; }
// other properties
}
Then in your service you could have a method to get the customer information, for example:
public Customer GetCustomerInfo(string customerID)
{
// Code to get customer data, populate CustomerInformation and return it.
}
In your client, you would then do:
MyServiceClient client = new MyServiceClient();
Customer customerInfo = client.GetCustomerInfo("12345");
The client would then access the various parts of the data in the same way you would any other instance of a class with properties:
string name = customerInfo.Name;
string city = customerInfo.Address.City;
string phone = customerInfo.Contact.EmailAddress;
No need to use enums here, simply work with the object returned by the proxy from your service as you would with any other C# class. Note that generally speaking, Data Contracts don't contain methods or constructors.
See also Using Data Contracts
On a final note, your service should implement the interface, and that is what the client will be built upon, not the interface - in other words, IsvcService data = new IsvcService();
should look like svcService data = new svcService();
, where svcService
implements the contract in the interface IsvcService
.
Upvotes: 1
Reputation: 11763
You are going to treat your WCF web service as you would any other class ... You will create a proxy object to it, and then call methods on it. For example:
var proxy = // you'll instantiate your proxy client here
proxy.CallFirstMethod();
proxy.CallSecondMethod("this takes a string");
// and so on.
Enums
in WCF are treated as normal, but you need to declare them in your model (just like any other data type), so WCF knows how to deal with them.
From what it seems, you don't need Enums
, you want to invoke methods on the service with some parameters.
For completeness, here's how you'll define an Enum
so WCF can work with it
[DataContract(Name = "MyFancyEnum")]
public enum FancyEnum
{
[EnumMember]
Unicorn,
[EnumMember]
Ogre,
[EnumMember]
Banana
}
Upvotes: 0