我使用WCF创建了一个Web服务,我想在silverlight项目中使用它。 这是我的web.config文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBindingConfig">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2252/Service1"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="DataRetrieverReference.IService1" name="BasicHttpBinding_IService1" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
这是我的C#代码,用于从Web服务获取数据。但它始终返回null。
private void button1_Click(object sender, RoutedEventArgs e)
{
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost:2252/Service1");
var myChannelFactory = new ChannelFactory<IService1>(binding, endpoint);
IService1 client = null;
Person person = null;
try
{
client = myChannelFactory.CreateChannel();
}
catch
{
}
person = client.GetPersonbyId(textBox1.Text);
name.Text = person.nameValue;
surname.Text = person.surnameValue;
}
[ServiceContract]
public interface IService1
{
Person GetPersonbyId(string id);
// TODO: Add your service operations here
}
我缺少什么?