|
blueskyc's blog
永不放弃 |
本章内容主要是根据我做的实验来阐述这2种添加服务针对WCF的不同之处,我们按照示例一步一步来看。
如下是工程的结构:

该WCF服务是通过控制台程序(Host)以自宿的形式发布的,绑定使用wsHttpBinding。我们在Client端分别添加
服务引用(add service references)和添加Web引用(add Web Reference )来引用WCF服务。
以下是客户端的代码,分别使用添加服务引用和添加Web引用的服务代理来调用WCF的方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Yingchao.Client.localhost; using Yingchao.Client.ServiceReference1; namespace Yingchao.Client { class Program { static void Main(string[] args) { // add service reference's proxy Service1Client client = new Service1Client(); Console.WriteLine(client.GetData(111)); // add web reference's proxy Service1 s = new Service1(); Console.WriteLine(s.GetData(1234, true)); Console.Read(); } } }
客户端配置文件:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="Yingchao.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <system.serviceModel> <client> <!-- 添加服务引用时自动生成 --> <endpoint address="http://localhost:8732/service" binding="wsHttpBinding" contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> <applicationSettings> <!-- 添加Web服务引用时自动生成 --> <Yingchao.Client.Properties.Settings> <setting name="Yingchao_Client_localhost_Service1" serializeAs="String"> <value>http://localhost:8732/service</value> </setting> </Yingchao.Client.Properties.Settings> </applicationSettings> </configuration>
服务端配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <services> <service name="Yingchao.Service.Service1"> <host> <baseAddresses> <add baseAddress = "http://localhost:8732/service" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否则地址将与上面提供的基址相关 --> <endpoint address ="" binding="wsHttpBinding" contract="Yingchao.Contract.IService1"> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
我们启动服务后,运行客户端,我们看看结果是什么:

我们看到这里添加Web服务代理调用WCF的方法的结果没有显示出来,而是出现了"操作超时"错误。
那我们更改服务端配置文件的绑定:wsHttpBinding 改成 basicHttpBinding,编译后更新引用的服务。
然后再次运行客户端,我们看看结果:

我们看到这次2个引用服务都成功调用。可见添加Web服务应该只能使用basicHttpBinding,也许微软是为了向前兼容留下的。
然后,分别添加的服务引用生成的Reference.cs里面生成的代码也不一样。添加服务引用更偏向WCF规则。
我查资料也发现跟我想的差不多.(http://social.microsoft.com/Forums/zh-CN/xmlwebserviceszhchs/thread/808d870b-49f1-47ac-b105-4beb580bcec6)
学习WCF的时候,寄宿于IIS研究了很长时间,都一直没成功,一直都是IIS错误画面游荡中…… 很郁闷很郁闷的。偶然想到是否因为WCF注册的原因?
于是MSDN找到WCF注册的资料(http://msdn.microsoft.com/zh-cn/library/ms732012(v=vs.90).aspx),运用命令进行重新注册。
步骤如下:
1,开始菜单中打开Visual Studio 2008 Command Prompt (Visual Studio 2008命令提示符);
2,跳转到C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation目录中;
3,ServiceModelReg.exe -r -y。
4,注册完之后,再访问WCF服务就可以了。
如果出现MIME错误的,可能是因为没有svc后缀,那我们加上试试,
步骤:
1,我们在C:\Windows\System32\Inetsrv\Config\目录中,找到applicationHost.config文件;
2,在staticContent节点中,我们为.svc文件加入以下MIME条目
<mimeMap fileExtension=".svc" mimeType="application/octet-stream" />
3,保存关闭文件。
参考:http://technet.microsoft.com/en-us/library/dd632554.aspx。
在liunx中新建用户后,win7 使用ssh(cygwin)连接liunx的时候,一直出现问题,需要输入密码。我证书已经配置了,还是需要。 以前的用户是可以的。最终网上搜索解决了,原来是权限问题。
主要有三个权限
(1) /home/username这目录的权限应该是700
(2) /home/username/.ssh 这个目录的权限也应该是700
(3) /home/username/.ssh/ authorized_keys这个文件的权限是644
如果还有问题,那么tail /var/log/secure一下这个日志文件,就能发现问题。
摘自(http://blog.csdn.net/emili/archive/2009/02/01/3856622.aspx)
这里做备案,以备后用。
完整配置过程:
步骤:
1 ssh-keygen -t rsa 生成密匙
2 在liunx中当前用户文件夹下(/home/username/)创建.ssh文件夹
3 把本地生成的id_rsa.pub(xp:documents and Settings/.ssh中。)文件复制到liunx的.ssh文件夹下
4 把id_rsa.pub文件命名为authorized_keys2
现在就可以测试是否配置成功。
输入: ssh username@ip 回车。 如果直接进入liunx,那恭喜你配置成功。如果要求输入密码,那就说明没有配置好。
// number为四舍五入的数字
// keta是保留小数点之后的位数,从0开始 (0,1,-1)
// -1: 表示整数 (155->160) 1:(1.05 ->1.10) 0:(0.5->1.0)
public BigDecimal rounds(BigDecimal number, int keta) {
NumberFormat nbf=NumberFormat.getInstance();
nbf.setMinimumFractionDigits(keta+1);
BigDecimal value = new BigDecimal(nbf.format(number.setScale(keta, BigDecimal.ROUND_HALF_UP).doubleValue()));
return value;
}