0
プログラムで設定ファイルを使用するようにエンドポイントを設定しようとしています。問題は、configファイルを使用するとエラーは発生しませんが、tcpポートは開かず、クライアントは接続できないということです。net.tcpはプログラムで設定したときに機能しますが、設定ファイルを使用していないときには
これは、プログラムで設定されたときにポートが開かれていることを示すnetstatで、次に設定ファイルで表示されます。
次は私の完全な例クライアントサーバであると app.configを
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Server.CustomerService">
<endpoint
address="net.tcp://localhost:8081/CustomerService"
binding="netTcpBinding"
contract="Shared.ICustomerService"/>
</service>
</services>
</system.serviceModel>
</configuration>
サーバー契約:
class ProgramService
{
static List<Customer> _customers = new List<Customer>();
static void Main(string[] args)
{
CreateCustomers();
Uri netTCPObject = new Uri("net.tcp://localhost:8081/CustomerService");
ServiceHost sessionHost = new ServiceHost(typeof(CustomerService), netTCPObject);
//ServiceHost sessionHost = new ServiceHost("Server.CustomerService"); //using the app.config
sessionHost.Open();
Console.WriteLine("Service is running");
Console.ReadLine();
sessionHost.Close();
}
private static void CreateCustomers()
{
_customers.Add(new Customer() { CustomerId = 1, FirstName = "Fred", LastName = "Flintstone" });
_customers.Add(new Customer() { CustomerId = 2, FirstName = "John", LastName = "Doe" });
_customers.Add(new Customer() { CustomerId = 3, FirstName = "Rebecca", LastName = "Johndaughter" });
_customers.Add(new Customer() { CustomerId = 4, FirstName = "Julie", LastName = "Herald" });
}
public class CustomerService : ICustomerService
{
public Customer GetCustomer(int customerId)
{
return _customers.FirstOrDefault(c => c.CustomerId == customerId);
}
public bool UpdateCustomer(Customer customer)
{
var curCust = _customers.FirstOrDefault(c => c.CustomerId == customer.CustomerId);
if (curCust != null)
{
curCust.FirstName = customer.FirstName;
curCust.LastName = customer.LastName;
}
else
{
_customers.Add(customer);
}
return true;
}
}
}
契約:
namespace Shared
{
[ServiceContract()]
public interface ICustomerService
{
[OperationContract]
Customer GetCustomer(int customerId);
[OperationContract]
bool UpdateCustomer(Customer customer);
}
[DataContract]
public class Customer
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int CustomerId { get; set; }
}
}
クライアント:
をclass ProgramClient
{
static void Main(string[] args)
{
//http://stackoverflow.com/a/2943206/232226
NetTcpBinding binding = new NetTcpBinding();
EndpointAddress endpoint = new EndpointAddress("net.tcp://localhost:8081/CustomerService");
ChannelFactory<ICustomerService> factory = new ChannelFactory<ICustomerService>(binding, endpoint);
ICustomerService service = factory.CreateChannel();
for (int i = 1; i < 5; i++)
{
Customer customer = service.GetCustomer(i);
Console.WriteLine(String.Format(" Customer {0} {1} received.", customer.FirstName, customer.LastName));
}
Console.ReadLine();
}
}
:タイプのfull nameを使用し、
と設定でサービスのそれは ''でなければなりません。 –
CodeCaster
したがって、文字列コンストラクタを使用すると、任意の名前を使用することはできませんか?新しいServiceHost( "Server.CustomerService"); – ToddK
だから、私は示唆したように、設定で名前を変更し、ServiceHostコンストラクタで名前を更新しましたが、それは助けになりませんでした。この例を設定ファイルで実行するには、2番目のServiceHostインスタンスのコメントを外し、最初のコメントアウトを行う必要があります。 – ToddK