我有以下课程, DBConn用于数据库连接, UserBL类用于处理外部用户功能, SpeakerVO类用于处理实体属性, SpeakerBL类用于处理业务层方法, 用于服务层接口的ICustomerService, CustServiceManager类用于处理服务层方法
问题是当你调用服务层方法适当地传递User类时,但仍然是CustServiceManager类,ViewAll方法是_user 参数变为空
Windows应用程序中的客户端调用代码位于
之下public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UserBL _user = new UserBL();
string ConStrng = ConfigurationSettings.AppSettings["ConnectionString1"];
_user.SetConnection(ConStrng);
SpeakerReference.CustomerServiceClient x = new SpeakerReference.CustomerServiceClient();
x.ViewAll(_user);
}
}
namespace CodeCamper.DataLayer
{
[DataContract]
public class DBConn
{
private string mstr_username;
private string mstr_password;
private string mstr_server;
private string mstr_database;
private string mstr_connectionString;
private IDbConnection mcon_thisConnection;
[DataMember]
public IDbConnection MyConnection
{
get { return mcon_thisConnection; }
}
public DBConn(string connectionstr)
{
mcon_thisConnection = new SqlConnection(connectionstr);
}
}
}
namespace CodeCamper.BusinessLayer
{
[DataContract]
public class UserBL
{
private DBConn _DB;
//[DataMember]
public DBConn db
{
get { return _DB; }
set { _DB = value; }
}
public void SetConnection(string connectionstr)
{
_DB = new DBConn(connectionstr);
}
}
}
namespace CodeCamper.EntityLayer.Transaction
{
[DataContract]
public class SpeakerVO
{
private string _SpeakerID;
private string _SpeakerName;
private string _SpeakerSurname;
private string _Email;
private string _Twitter;
private string _Blog;
private string _Bio; //list like entry
private string _ImagePath;
[DataMember]
public string SpeakerID
{
get { return _SpeakerID; }
set { _SpeakerID = value; }
}
[DataMember]
public string SpeakerName
{
get { return _SpeakerName; }
set { _SpeakerName = value; }
}
[DataMember]
public string SpeakerSurname
{
get { return _SpeakerSurname; }
set { _SpeakerSurname = value; }
}
[DataMember]
public string Email
{
get { return _Email; }
set { _Email = value; }
}
[DataMember]
public string Twitter
{
get { return _Twitter; }
set { _Twitter = value; }
}
[DataMember]
public string Blog
{
get { return _Blog; }
set { _Blog = value; }
}
[DataMember]
public string Bio
{
get { return _Bio; }
set { _Bio = value; }
}
[DataMember]
public string ImagePath
{
get { return _ImagePath; }
set { _ImagePath = value; }
}
}
}
namespace CodeCamper.BusinessLayer.Transaction
{
[ServiceContract]
public class SpeakerBL : SpeakerVO
{
private UserBL _user;
#region -Constructor-
public SpeakerBL(UserBL user)
{
//ID = null;
//Code = string.Empty;
//Name = string.Empty;
_user = user;
}
#endregion
#region -Methods-
[OperationContract]
public static List<SpeakerVO> ViewAll(UserBL _user)
{
List<SpeakerVO> _tmpresult = null;
try
{
SpeakerDL ContainerTable = new SpeakerDL(_user.db, null);
_tmpresult = ContainerTable.ViewAll();
}
catch (Exception er)
{
throw er;
}
return _tmpresult;
}
#endregion
}
}
namespace CodeCamper.ServiceLayer
{
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
List<SpeakerVO> ViewAll(UserBL _user);
}
}
namespace CodeCamper.ServiceLayer
{
public class CustServiceManager : ICustomerService
{
public List<SpeakerVO> ViewAll(UserBL _user)
{
List<SpeakerVO> _tmpresult = null;
try
{
_tmpresult = SpeakerBL.ViewAll(_user);
}
catch (Exception ex)
{
throw ex;
}
return _tmpresult;
}
}
}
客户端配置文件位于
之下<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
<appSettings>
<add key="ConnectionString1" value="Data Source=C008;Initial Catalog=sample;Persist Security Info=True;User ID=sa;password=ssa@123"/>
</appSettings>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICustomerService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/CodeCamper.ServiceLayer/CustomerService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICustomerService"
contract="SpeakerReference.ICustomerService" name="BasicHttpBinding_ICustomerService" />
</client>
</system.serviceModel>
</configuration>
服务层配置文件位于
之下<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true"/>
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="CodeCamper.ServiceLayer.CustServiceManager">
<endpoint address="" binding="basicHttpBinding" contract="CodeCamper.ServiceLayer.ICustomerService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/CodeCamper.ServiceLayer/CustomerService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="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>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>