Help!Why can't I access the WCF Service from a client winform application?
1.The file service.cs under the folder "App_Code" is like this:
[ServiceContract(Namespace="http://www.ailayer.com")]
public interface ILoginService
{
[OperationContract]
bool Login(string userName,string password);
}
public class LoginService : ILoginService
{
#region ILoginService Members
//[PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
public bool Login(string userName, string password)
{
return true;
}
#endregion
}
--
2.The file service.svc is like this:
<% @ServiceHost Language=C# Debug="true" Service="LoginService" CodeBehind="~/App_Code/Service.cs" %>
3.The Web.config is like this:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service name="LoginService" behaviorConfiguration="serviceBehavior">
<endpoint contract="ILoginService" binding="basicHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior" >
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
-
4.The Winform project "Client" contains a Form "LoginForm" which contains two textbox for username and password,also a button which is used to invoke the service.I add the service reference to this project. The Form code is like this:
public partial class LogInForm : Form
{
LoginServiceClient proxyLogin = null;
// 初始化
public LogInForm()
{
InitializeComponent();
}
// 窗体数据加载
private void LogInForm_Load(object sender, EventArgs e)
{
this.proxyLogin = new LoginServiceClient();
}
// 用户登录
private void btnLogIn_Click(object sender, EventArgs e)
{
try
{
bool log = this.proxyLogin.Login(this.txtUserName.Text,this.txtPassword.Text);
if(log)
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("用户名或密码错误,请重新输入");
this.txtPassword.Text = "";
}
}
catch(Exception ce)
{
MessageBox.Show(ce.Message);
}
finally
{
this.proxyLogin.Close();
}
}
}
--
5.The generated file app.config is like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ILoginService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="Ntlm"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://<My Domain name>/ServiceLayer/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ILoginService"
contract="ILoginService" name="BasicHttpBinding_ILoginService" />
</client>
</system.serviceModel>
</configuration>
--
6.There is still another file serviceproxy.cs generated which is like this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://www.ailayer.com", ConfigurationName="ILoginService")]
public interface ILoginService
{
[System.ServiceModel.OperationContractAttribute(Action="http://www.ailayer.com/ILoginService/Login", ReplyAction="http://www.ailayer.com/ILoginService/LoginResponse")]
bool Login(string userName, string password);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ILoginServiceChannel : ILoginService, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class LoginServiceClient : System.ServiceModel.ClientBase<ILoginService>, ILoginService
{
public LoginServiceClient()
{
}
public LoginServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public LoginServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public LoginServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public LoginServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public bool Login(string userName, string password)
{
return base.Channel.Login(userName, password);
}
}
-- I don't know what does this mean,and I have worked on it for a whole day,please help me!Thanks!!!
7.I set the project "Client" as the startup project.After running the
project,click the button ,it occurs the following exception:
The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

