I'm new to WCF and i created a service that contains a DataContract that is supposed to keep a datamember state but after i call getdata the state i get back is null. Though when i use tcp the value is preseved.
[ServiceContract(Namespace = "http://KamiServiceHost.net/Go")]
public interface IService1
{
[OperationContract]
DataItem GetData();
[OperationContract]
void SaveData(DataItem item);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
private DataItem _item1;
private DataItem item
{
get { return _item1; }
set { _item1 = value; }
}
public void SaveData(DataItem setitem)
{
item = setitem;
}
public DataItem GetData()
{
return item;
}
}
Host app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="Service.Service1" behaviorConfiguration="myServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000"/>
<add baseAddress="net.tcp://localhost:9000"/>
</baseAddresses>
</host>
<endpoint address="Service1" binding="basicHttpBinding" contract="Service.IService1"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
client code:
client.Open();
var dataItem = new DataItem() { Name = "my object", Number = 12 };
client.SaveData(dataItem);
var data = client.GetData();
Console.WriteLine(string.Format("Name: {0} Number: {1}", data.Name, data.Number));
the value of data is null and i have no idea why.. any help will be appreciated.
Thanks
BasicHttpBinding doesn't support that instance mode: http://msdn.microsoft.com/en-us/library/ms730879.aspx . With no session support, the object instance that you provide on the first call is lost
如果将服务设置为基于实例,则对于每个调用,您将在服务器上获得服务类的另一个实例,因此返回的项目当然为null。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.