我正在按照此Tuto在SQL服务器和C#应用程序之间部署WCF:https://www.youtube.com/watch?v=OGItMZAWkHg
所以这是我程序的一部分:
IService1:
namespace WCFCrud
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
Int32 Insert(WCFContact value);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class WCFContact
{
private Int32 contactID;
private String contactName;
private String contactAddress;
private String contactNumber;
private DateTime contactDOB;
private Decimal contactIncome;
[DataMember]
public Int32 ContactID
{
get
{
return contactID;
}
set
{
contactID = value;
}
}
[DataMember]
public string ContactName
{
get
{
return contactName;
}
set
{
contactName = value;
}
}
[DataMember]
public string ContactAddress
{
get
{
return contactAddress;
}
set
{
contactAddress = value;
}
}
[DataMember]
public string ContactNumber
{
get
{
return contactNumber;
}
set
{
contactNumber = value;
}
}
[DataMember]
public DateTime ContactDOB
{
get
{
return contactDOB;
}
set
{
contactDOB = value;
}
}
[DataMember]
public decimal ContactIncome
{
get
{
return contactIncome;
}
set
{
contactIncome = value;
}
}
}
}
我的Service1.cs:
namespace WCFCrud
{
public class Service1 : IService1
{
string ConString = "Server="ServerAdress";Database="DatabaseName";Trusted_Connection=True;";
public int Insert(WCFContact ContactPar)
{
using (SqlConnection con = new SqlConnection(ConString))
{
using (SqlCommand cmd = new SqlCommand("dbo.InsertContact", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ContactName", SqlDbType.VarChar, 100).Value = ContactPar.ContactName;
cmd.Parameters.Add("@ContactAddress", SqlDbType.VarChar, 100).Value = ContactPar.ContactAddress;
cmd.Parameters.Add("@ContactNumber", SqlDbType.VarChar, 100).Value = ContactPar.ContactNumber;
cmd.Parameters.Add("@ContactDOB", SqlDbType.DateTime).Value = ContactPar.ContactDOB;
cmd.Parameters.Add("@ContactIncome", SqlDbType.Money).Value = ContactPar.ContactIncome;
con.Open();
var ID = cmd.ExecuteScalar();
ContactPar.ContactID = Convert.ToInt32(ID.ToString());
}
}
return ContactPar.ContactID;
}
public int Update(WCFContact ContactPar)
{
using (SqlConnection con = new SqlConnection(ConString))
{
using (SqlCommand cmd = new SqlCommand("dbo.UpdateContact", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ContactID", SqlDbType.Int).Value = ContactPar.ContactID;
cmd.Parameters.Add("@ContactName", SqlDbType.VarChar, 100).Value = ContactPar.ContactName;
cmd.Parameters.Add("@ContactAddress", SqlDbType.VarChar, 100).Value = ContactPar.ContactAddress;
cmd.Parameters.Add("@ContactNumber", SqlDbType.VarChar, 100).Value = ContactPar.ContactNumber;
cmd.Parameters.Add("@ContactDOB", SqlDbType.DateTime).Value = ContactPar.ContactDOB;
cmd.Parameters.Add("@ContactIncome", SqlDbType.Money).Value = ContactPar.ContactIncome;
con.Open();
var ID = cmd.ExecuteNonQuery();
return ID;
}
}
}
public List<WCFContact> SelectAll()
{
List<WCFContact> SelectList = new List<WCFContact>();
using (SqlConnection con = new SqlConnection(ConString))
{
using (SqlCommand cmd = new SqlCommand("dbo.SelectAllContact", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
SelectList.Add(new WCFContact
{
ContactAddress = dr["ContactAddress"].ToString(),
ContactDOB = Convert.ToDateTime(dr["ContactDOB"]),
ContactID = Convert.ToInt32(dr["ContactID"]),
ContactIncome = Convert.ToDecimal(dr["ContactIncome"]),
ContactName = Convert.ToString(dr["ContactName"]),
ContactNumber = Convert.ToString(dr["ContactNumber"])
});
}
}
}
}
}
return SelectList;
}
public int Delete(WCFContact ContactPar)
{
using (SqlConnection con = new SqlConnection(ConString))
{
using (SqlCommand cmd = new SqlCommand("dbo.DeleteContact", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ContactID", SqlDbType.Int).Value = ContactPar.ContactID;
con.Open();
var ID = cmd.ExecuteNonQuery();
return ID;
}
}
}
}
}
我的控制台应用程序:
namespace WCFCrudConsole
{
class Program
{
static void Main(string[] args)
{
ServiceRef.Service1Client objClient = new ServiceRef.Service1Client();
ServiceRef.WCFContact objContact = new ServiceRef.WCFContact();
objContact.ContactName = "Dominic";
objContact.ContactNumber = "1234567";
objContact.ContactIncome = 50000;
objContact.ContactDOB = DateTime.Now;
var ID = objClient.Insert(objContact);
}
}
}
和我的应用程序配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:20570/Service1.svc?singleWsdl" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceRef.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
经过一番搜索,我找到了一个可以更改我的行的主题
http:// localhost:20570 / Service1.svc,但没有任何更改
那么,如果有人对我该怎么办有所了解?
提前谢谢了
最佳答案
您必须将端点名称作为参数传递给ServiceRef.Service1Client objClient = new ServiceRef.Service1Client();。
EndpointNotFoundException仅在客户端找不到时发生,因此我们必须显式传递参数名称。这个对我有用