Adding classes to WCF service library

狂风中的少年 提交于 2021-01-29 06:31:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!