csharp WCF示例:GeoLib

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WCF示例:GeoLib相关的知识,希望对你有一定的参考价值。

namespace GeoLib.WindowsHost
{
    
    public partial class MainWindow : Window
    {
        public static MainWindow MainUI { get; set; }
        public MainWindow()
        {
            InitializeComponent();

            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;

            MainUI = this;

            this.Title = "UI Running on Thread " + Thread.CurrentThread.ManagedThreadId +
                " | Process " + Process.GetCurrentProcess().Id.ToString();

            _SyncContext = SynchronizationContext.Current;
        }

        SynchronizationContext _SyncContext = null;
        ServiceHost _HostGeoManager = null;
        ServiceHost _HostMessageManager = null;
        
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            _HostGeoManager = new ServiceHost(typeof(GeoManager));
            _HostMessageManager = new ServiceHost(typeof(MessageManager));

            _HostGeoManager.Open();
            _HostMessageManager.Open();

            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            _HostGeoManager.Close();
            _HostMessageManager.Close();

            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;
        }

        public void ShowMessage(string message)
        {
            int threadId = Thread.CurrentThread.ManagedThreadId;

            //lblMessage.Content = message + Environment.NewLine + " (shown on thread " + threadId.ToString() + ")";

            SendOrPostCallback callback = new SendOrPostCallback(arg =>
            {
                lblMessage.Content = message + Environment.NewLine + " (marshalled from thread " + threadId + " to thread " +
                    Thread.CurrentThread.ManagedThreadId.ToString() + " | Process " + Process.GetCurrentProcess().Id.ToString() + ")";
            });

            _SyncContext.Send(callback, null);
        }

        private void btnInProc_Click(object sender, RoutedEventArgs e)
        {
            #region before
            //EndpointAddress address = new EndpointAddress("net.pipe://localhost/MessageService");
            //Binding binding = new NetNamedPipeBinding();

            //ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, address);

            //IMessageService proxy = factory.CreateChannel();

            //proxy.ShowMessage(DateTime.Now.ToLongTimeString() + " from in-process call.");

            //factory.Close();
            #endregion

            Thread thread = new Thread(() =>
            {
                EndpointAddress address = new EndpointAddress("net.pipe://localhost/MessageService");
                Binding binding = new NetNamedPipeBinding();

                ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, address);

                IMessageService proxy = factory.CreateChannel();

                proxy.ShowMessage(DateTime.Now.ToLongTimeString() + " from in-process call.");

                factory.Close();
            });

            thread.IsBackground = true;
            thread.Start();
        }
    }
}
namespace GeoLib.Tests
{
    [TestClass]
    public class ServiceTests
    {
        [TestMethod]
        public void test_zip_code_retrieval()
        {
            string address = "net.pipe://localhost/GeoService";
            Binding binding = new NetNamedPipeBinding();

            ServiceHost host = new ServiceHost(typeof(GeoManager));

            host.AddServiceEndpoint(typeof(IGeoService), binding, address);

            host.Open();

            ChannelFactory<IGeoService> factory = new ChannelFactory<IGeoService>(binding, new EndpointAddress(address));
            IGeoService proxy = factory.CreateChannel();

            ZipCodeData data = proxy.GetZipInfo("07035");

            Assert.IsTrue(data.City.ToUpper() == "LINCOLN PARK");
            Assert.IsTrue(data.State == "NJ");
        }
    }
}
[assembly: ContractNamespace("http://www.pluralsight.com/MiguelCastro/WcfEndToEnd",
                              ClrNamespace = "GeoLib.Client.Contracts")]
namespace GeoLib.Proxies
{
    public class GeoClient : ClientBase<IGeoService>, IGeoService
    {
        public GeoClient(string endpointName)
            : base(endpointName)
        {            
        }

        public GeoClient(Binding binding, EndpointAddress address)
            : base(binding, address)
        {            
        }

        public ZipCodeData GetZipInfo(string zip)
        {
            return Channel.GetZipInfo(zip);
        }

        public IEnumerable<string> GetStates(bool primaryOnly)
        {
            return Channel.GetStates(primaryOnly);
        }

        public IEnumerable<ZipCodeData> GetZips(string state)
        {
            return Channel.GetZips(state);
        }

        public IEnumerable<ZipCodeData> GetZips(string zip, int range)
        {
            return Channel.GetZips(zip, range);
        }
    }
}
  <system.serviceModel>
    <client>
      <endpoint address="net.tcp://localhost:8009/GeoService"
                binding="netTcpBinding"
                contract="GeoLib.Contracts.IGeoService"
                name="tcpEP" />
        <endpoint address="http://localhost/GeoService"
                  binding="basicHttpBinding"
                  contract="GeoLib.Contracts.IGeoService"
                  name="httpEP" />
      <endpoint address="http://localhost:4497/GeoService"
                binding="wsHttpBinding"
                contract="GeoLib.Contracts.IGeoService"
                name="webEP" />

      <endpoint address="net.tcp://localhost:8010/MessageService"
                binding="netTcpBinding"
                contract="GeoLib.Client.Contracts.IMessageService" />
    </client>
  </system.serviceModel>
namespace GeoLib.Client
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Title = "UI Running on Thread " + Thread.CurrentThread.ManagedThreadId +
                " | Process " + Process.GetCurrentProcess().Id.ToString();
        }

        private void btnGetInfo_Click(object sender, RoutedEventArgs e)
        {
            if (txtZipCode.Text != "")
            {
                GeoClient proxy = new GeoClient("tcpEP");

                ZipCodeData data = proxy.GetZipInfo(txtZipCode.Text);
                if (data != null)
                {
                    lblCity.Content = data.City;
                    lblState.Content = data.State;
                }

                proxy.Close();
            }
        }

        private void btnGetZipCodes_Click(object sender, RoutedEventArgs e)
        {
            if (txtState.Text != null)
            {
                EndpointAddress address = new EndpointAddress("net.tcp://localhost:8009/GeoService");
                Binding binding = new NetTcpBinding();

                GeoClient proxy = new GeoClient(binding, address);
                IEnumerable<ZipCodeData> data = proxy.GetZips(txtState.Text);
                if (data != null)
                    lstZips.ItemsSource = data;

                proxy.Close();
            }
        }

        private void btnMakeCall_Click(object sender, RoutedEventArgs e)
        {
            EndpointAddress address = new EndpointAddress("net.tcp://localhost:8010/MessageService");
            Binding binding = new NetTcpBinding();

            ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>(binding, address);

            //ChannelFactory<IMessageService> factory = new ChannelFactory<IMessageService>("");
            IMessageService proxy = factory.CreateChannel();

            proxy.ShowMessage(txtMessage.Text);

            factory.Close();
        }
    }
}
  <system.serviceModel>
    <services>
      <service name="GeoLib.Services.GeoManager">
        <endpoint address="net.tcp://localhost:8009/GeoService" 
                  binding="netTcpBinding" 
                  contract="GeoLib.Contracts.IGeoService" />
      </service>
    </services>
  </system.serviceModel>
namespace GeoLib.WindowsHost
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;

            this.Title = "UI Running on Thread " + Thread.CurrentThread.ManagedThreadId;
        }

        ServiceHost _HostGeoManager = null;

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            _HostGeoManager = new ServiceHost(typeof(GeoManager));
            _HostGeoManager.Open();

            btnStart.IsEnabled = false;
            btnStop.IsEnabled = true;
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            _HostGeoManager.Close();

            btnStart.IsEnabled = true;
            btnStop.IsEnabled = false;
        }
    }
}
  <system.serviceModel>
    <services>
      <service name="GeoLib.Services.GeoManager">
        <endpoint address=""
                  binding="wsHttpBinding" 
                  contract="GeoLib.Contracts.IGeoService" />
      </service>
    </services>
    <serviceHostingEnvironment>
      <serviceActivations>
        <add service="GeoLib.Services.GeoManager" 
             relativeAddress="GeoService.svc" 
             factory="GeoLib.WebHost.CustomHostFactory" />
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
namespace GeoLib.WebHost
{
    public class CustomHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            ServiceHost host = new ServiceHost(serviceType, baseAddresses);

            return host;
        }
    }
}
  <system.serviceModel>
    <services>
      <service name="GeoLib.Services.GeoManager">
        <endpoint address="net.tcp://localhost:8009/GeoService"
                  binding="netTcpBinding" 
                  contract="GeoLib.Contracts.IGeoService" />
      </service>
    </services>
  </system.serviceModel>
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost hostGeoManager = new ServiceHost(typeof(GeoManager));

            //string address = "net.tcp://localhost:8009/GeoService";
            //Binding binding = new NetTcpBinding();
            //Type contract = typeof(IGeoService);

            //hostGeoManager.AddServiceEndpoint(contract, binding, address);

            hostGeoManager.Open();

            Console.WriteLine("Services started. Press [Enter] to exit.");
            Console.ReadLine();

            hostGeoManager.Close();
        }
    }
namespace GeoLib.Services
{
    public class GeoManager : IGeoService
    {
        public GeoManager()
        {
        }

        public GeoManager(IZipCodeRepository zipCodeRepository)
            : this(zipCodeRepository, null)
        {
        }

        public GeoManager(IStateRepository stateRepository)
            : this(null, stateRepository)
        {            
        }

        public GeoManager(IZipCodeRepository zipCodeRepository, IStateRepository stateRepository)
        {
            _ZipCodeRepository = zipCodeRepository;
            _StateRepository = stateRepository;
        }

        IZipCodeRepository _ZipCodeRepository = null;
        IStateRepository _StateRepository = null;

        public ZipCodeData GetZipInfo(string zip)
        {
            ZipCodeData zipCodeData = null;

            IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository();

            ZipCode zipCodeEntity = zipCodeRepository.GetByZip(zip);
            if (zipCodeEntity != null)
            {
                zipCodeData = new ZipCodeData()
                {
                    City = zipCodeEntity.City,
                    State = zipCodeEntity.State.Abbreviation,
                    ZipCode = zipCodeEntity.Zip
                };
            }

            return zipCodeData;
        }

        public IEnumerable<string> GetStates(bool primaryOnly)
        {
            List<string> stateData = new List<string>();

            IStateRepository stateRepository = _StateRepository ?? new StateRepository();

            IEnumerable<State> states = stateRepository.Get(primaryOnly);
            if (states != null)
            {
                foreach (State state in states)
                    stateData.Add(state.Abbreviation);
            }

            return stateData;
        }

        public IEnumerable<ZipCodeData> GetZips(string state)
        {
            List<ZipCodeData> zipCodeData = new List<ZipCodeData>();

            IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository();

            var zips = zipCodeRepository.GetByState(state);
            if (zips != null)
            {
                foreach (ZipCode zipCode in zips)
                {
                    zipCodeData.Add(new ZipCodeData()
                    {
                        City = zipCode.City,
                        State = zipCode.State.Abbreviation,
                        ZipCode = zipCode.Zip
                    });
                }
            }

            return zipCodeData;
        }

        public IEnumerable<ZipCodeData> GetZips(string zip, int range)
        {
            List<ZipCodeData> zipCodeData = new List<ZipCodeData>();

            IZipCodeRepository zipCodeRepository = _ZipCodeRepository ?? new ZipCodeRepository();

            ZipCode zipEntity = zipCodeRepository.GetByZip(zip);
            IEnumerable<ZipCode> zips = zipCodeRepository.GetZipsForRange(zipEntity, range);
            if (zips != null)
            {
                foreach (ZipCode zipCode in zips)
                {
                    zipCodeData.Add(new ZipCodeData()
                    {
                        City = zipCode.City,
                        State = zipCode.State.Abbreviation,
                        ZipCode = zipCode.Zip
                    });
                }
            }

            return zipCodeData;
        }
    }
}
namespace GeoLib.Contracts
{
    [DataContract]
    public class ZipCodeData
    {
        [DataMember]
        public string City { get; set; }
        [DataMember]
        public string State { get; set; }
        [DataMember]
        public string ZipCode { get; set; }
    }
}
namespace GeoLib.Contracts
{
    [ServiceContract]
    public interface IGeoService
    {
        [OperationContract]
        ZipCodeData GetZipInfo(string zip);

        [OperationContract]
        IEnumerable<string> GetStates(bool primaryOnly);

        [OperationContract(Name = "GetZipsByState")]
        IEnumerable<ZipCodeData> GetZips(string state);

        [OperationContract(Name = "GetZipsForRange")]
        IEnumerable<ZipCodeData> GetZips(string zip, int range);
    }
}

以上是关于csharp WCF示例:GeoLib的主要内容,如果未能解决你的问题,请参考以下文章

csharp 的Epicor-WCF-j.cs

csharp 创建Epicor BO Instace WCF

geolib 在 react-native 状态下尚未准备好地理位置坐标

TypeError:无法读取 Geolib.isDecimal 未定义的属性“toString”

geolib在反应原生状态下未准备好地理位置坐标

csharp Erik Johnson来自Epicor的BO和WCF