标签:
1.环境设定
工作目录:
D:\workspace\ (目录可随意)
需要的应用程序:
SvcUtil.exe(解析wcf的url生成cs和config文件的工具)来源:有vs.net的PC的C盘中搜索出来,复制到D:\workspace\中。
csc.exe(通过cs文件生成dll的工具)来源:C盘中搜索出来,不复制。
需要的DLL:
System.ServiceModel.dll 来源:有framework的PC的C盘中搜索出来(版本号要和SvcUtil.exe生成的cs文件需要的一致),复制到D:\workspace\中。
2.命令行
(粗体:命令行; 粗体斜体:可替换的WCF参数; 红字:说明; 其他:控制台输出)
#跳转到工作目录
PS C:\Users> cd D:\workspace\
#解析wcf的url生成cs和config文件
PS D:\workspace> .\SvcUtil.exe https://test.xxxxxxxxxxx.com/API_v1/WCFService.svc
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.648]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from ‘https://test.xxxxxxxxxxx.com/API_v1/WCFService.svc‘ using WS-Metadat
a Exchange or DISCO.
Generating files...
D:\workspace\WCFService.cs
D:\workspace\output.config
#引用System.ServiceModel.dll编译WCFService.cs生成WCFService.dll
PS D:\workspace> C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:library WCFService.cs /r:”.\System.ServiceModel.dll”
适用于 Microsoft(R) .NET Framework 3.5 版的 Microsoft(R) Visual C# 2008 编译器 3.5.30729.5420
版
版权所有(C) Microsoft Corporation。保留所有权利。
#当前路径保存
PS D:\workspace> $current_directory = Get-Location
#当前路径显示
PS D:\workspace> $current_directory
Path
----
D:\workspace
#加载System.ServiceModel.dll
PS D:\workspace> [System.Reflection.Assembly]::LoadFrom(“$current_directory\System.ServiceModel.dll”)
GAC Version Location
--- ------- --------
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\...
#加载WCFService.dll
PS D:\workspace> [System.Reflection.Assembly]::LoadFrom(“$current_directory\WCFService.dll”)
GAC Version Location
--- ------- --------
False v2.0.50727 D:\workspace\WCFService.dll
#设置环境的配置config文件
PS D:\workspace> [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", “$current_directory\output.config”)
PS D:\workspace> if ($psISE -ne $null) {
>> Add-Type -AssemblyName System.Configuration
>> [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null,0)
>> [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null,$null)
>> ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientCo
nfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)
>> }
>>
#连接WCF,取得服务对象
#(WCFServiceClient是WCFService.cs中的类名,是System.ServiceModel.ClientBase的子类)
#(“CustomBinding_IWCFService”是output.config的endpoint元素name属性的值)
PS D:\workspace> $service = new-object WCFServiceClient("CustomBinding_IWCFService")
#设置验证用户名(服务端提供)
PS D:\workspace> $service.ClientCredentials.UserName.UserName="user001"
#设置验证密码(服务端提供)
PS D:\workspace> $service.ClientCredentials.UserName.Password="password001"
#调用服务对象方法(参数有复杂类型时,复杂类型定义在WCFService.cs中,实例化后传入下面的方法)
PS D:\workspace> $service.GetInfo("001","1")
※转载请注明出处http://i.cnblogs.com/EditPosts.aspx?postid=5316333
使用powershell通过配置文件config调用wcf(含用户名密码认证)
标签:
原文地址:http://www.cnblogs.com/whitecf/p/5316333.html