问题
I have a class that defines a transaction that needs to be shared between two separate applications. They both have references to this library and can use the class as a data type, but cannot invoke any of its methods:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ServerLibrary.MarketService;
namespace ServerLibrary
{
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
[OperationContract]
bool ProcessTransaction(Transaction transaction);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
// Transaction class to encapsulate products and checkout data
[DataContract]
public class Transaction
{
[DataMember]
public int checkoutID;
[DataMember]
public DateTime time;
[DataMember]
public List<Product> products;
[DataMember]
public double totalPrice;
[DataMember]
public bool complete;
public void Start(int ID)
{
checkoutID = ID;
products = new List<Product>();
complete = false;
}
public void Complete()
{
time = DateTime.Now;
complete = true;
}
}
}
What am I doing wrong?
[UPDATE] I missed out the rest of the service.
Thanks.
回答1:
In order to use the same .NET types from both the client and the server what you need to do is to add the data contract classes to a shared assembly that both the client and the host use. Then when your host is up and running and you do an add service reference there should be a checkbox that says reuse types from existing assembly.
This will make WCF recreate your objects and with the methods and data that you are expecting.
来源:https://stackoverflow.com/questions/10374487/adding-classes-to-wcf-service-library