[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using System.ServiceModel;
namespace Contracts
{
[ServiceContract]
public interface ICalculator
{
//四角形の面積を計算します
[OperationContract]
double GetArea(Rectangle rect);
}
//Error!
//(throws InvalidDataContractException)
public class Rectangle
{
public double Width;
public double Height;
}
}
using System.ServiceModel;
namespace Contracts
{
[ServiceContract]
public interface ICalculator
{
//四角形の面積を計算します
[OperationContract]
double GetArea(Rectangle rect);
}
[System.Serializable]
public class Rectangle
{
public double Width;
public double Height;
}
}
using System;
using System.ServiceModel;
using Contracts;
namespace Server
{
class MyCalculator : ICalculator
{
public double GetArea(Rectangle rect)
{
return rect.Width * rect.Height;
}
}
class Program
{
static void Main(string[] args)
{
ServiceHost serviceHost = new ServiceHost(typeof(MyCalculator));
serviceHost.AddServiceEndpoint(
typeof(ICalculator),
new NetTcpBinding(),
"net.tcp://localhost:8001/Calculator"
);
serviceHost.Open();
Console.WriteLine("サービスがオープンしました。終了するにはEnterを押してください。");
Console.ReadLine();
serviceHost.Close();
}
}
}
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Contracts;
namespace Client
{
class Program
{
static void Main(string[] args)
{
ICalculator remoteService = new ChannelFactory (
new NetTcpBinding(),
"net.tcp://localhost:8001/Calculator"
).CreateChannel();
Console.WriteLine(
"2 * 3 = "
+ remoteService.GetArea(new Rectangle{Width = 2, Height = 3})
);
((IChannel)remoteService).Close();
}
}
}
using System.ServiceModel;
namespace Contracts
{
[ServiceContract]
public interface ICalculator
{
//四角形の面積を計算します
[OperationContract]
double GetArea(Rectangle rect);
}
[System.Runtime.Serialization.DataContract]
public class Rectangle
{
[System.Runtime.Serialization.DataMember]
public double Width;
[System.Runtime.Serialization.DataMember]
public double Height;
}
}