我創建了一個項目作爲一個類庫。現在我需要把它變成一個WCF。我可以創建一個WCF項目,但我想避免與TFS大驚小怪。我已經完成了App.config並將/client:"wcfTestClient.exe「行添加到命令行參數中。但是似乎還有其他東西在啓動主機時丟失了。我需要採取什麼步驟將類庫轉換爲WCF?
5
A
回答
0
WCF不是網點。要創建一個WCF應用程序,你必須做四件事情
- 定義合同
- 服務實現在服務器端
- 主機您實現的服務
- 合同創建也可以使用該服務的客戶端合同
看看這個tutorial
氏s是服務的一個完整的例子,它的主機
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System;
[ServiceContract]
public interface AddStuff
{
[OperationContract]
int Add(int X,int Y);
}
public class opAddStuff : AddStuff
{
public int Add(int X, int Y)
{
return X + Y;
}
}
public class Pgm
{
static void Main(string[] args)
{
string httpAddr = "http://127.0.0.1:6001/AddStuff";
string netAddr= "net.tcp://127.0.0.1:5001/AddStuff";
System.ServiceModel.ServiceHost SH = new ServiceHost(typeof(opAddStuff),new Uri(httpAddr));
BasicHttpBinding B = new BasicHttpBinding();
NetTcpBinding NB = new NetTcpBinding();
SH.AddServiceEndpoint(typeof(AddStuff), B, httpAddr);
SH.AddServiceEndpoint(typeof(AddStuff), NB, netAddr);
System.ServiceModel.Description.ServiceMetadataBehavior smb = SH.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
SH.Description.Behaviors.Add(smb);
SH.AddServiceEndpoint( ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
SH.Open();
Console.WriteLine("Service at your service");
string crap = Console.ReadLine();
}
}
您也可以運行此命令
的netsh的http添加urlacl URL = http://+:6001/AddStuff用戶=域\用戶
一些這個來自here
12
我發現下面的做法與您正在嘗試實現的相反,即將服務庫更改爲控制檯應用程序。
csproj文件中的某些設置不能從VS中的設置屏幕進行編輯,以將類庫轉換爲WCF服務庫,您需要將以下內容添加到項目文件中
將以下內容添加到第一個PropertyGroup
[這是一個C#WCF項目的GUID]
<ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
在這裏看到進一步的信息,ProjectTypeGuids
您可能還需要添加以下行正下方:
<StartArguments>/client:"WcfTestClient.exe"</StartArguments>
但最終它是您需要手動插入的PropertyTypeGuids讓VS將項目識別爲WCF服務庫項目。
1
這是我不得不做的將我的類庫轉換爲WCF REST應用程序。
1)修改.csproj文件,並將以下兩行添加到.csproj文件中的第一個PropertyGroup元素。
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<UseIISExpress>false</UseIISExpress>
2)下面的行添加到下面<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
導入Microsoft.WebApplication.targets文件
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
3)</Project>
標記之前將以下代碼添加到該文件的末尾。
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>False</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>50178</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>
</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
4)保存的.csproj文件和刷新該項目。
5)將Web.Config文件添加到項目中,並添加下面的最小代碼。您可以根據您的要求添加更多內容。
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
6)添加Global.asax文件。以下是一個示例文件。
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
}
private void RegisterRoutes()
{
// Edit the base address of Service1 by replacing the "Service1" string below
RouteTable.Routes.Add(new ServiceRoute("YourService", new WebServiceHostFactory(), typeof(YourServiceClass)));
}
}
7)最後在項目的屬性,Build選項卡下,如果輸出路徑設置爲bin\Debug
其修改爲bin\
。
相關問題
- 1. 我需要採取哪些步驟來使用WCF回調?
- 2. 步驟在將Web應用程序轉換爲iPad時需要的步驟
- 3. 什麼步驟做我需要調用數據庫
- 4. 爲什麼我需要轉換?
- 5. 我需要採取什麼步驟,而從http更改爲https我的WordPress的網站
- 6. 使用NUnit需要什麼步驟?
- 7. 在Python中將視頻轉換爲Gif所需的步驟是什麼?
- 8. 爲什麼我們需要WCF
- 9. 需要採取哪些步驟在設備上實現OpenCL?
- 10. 爲什麼wcf duplex需要?
- 11. 爲什麼我需要這個CString類型轉換?
- 12. 解析Qt中的Asterix,爲什麼我需要類型轉換?
- 13. 爲什麼我們需要在C#中類型轉換枚舉
- 14. 將大類從靜態轉換爲非靜態的步驟是什麼?
- 15. 將qt3轉換爲qt4的步驟
- 16. 我需要做些什麼才能將DIB轉換爲BMP?
- 17. 爲什麼需要XML轉換?
- 18. 爲什麼需要顯式轉換?
- 19. Javascript,類型轉換需要(我認爲)
- 20. 爲什麼在TFS中構建步驟之後我們需要SonarQube結束分析步驟?
- 21. 我需要將SQL轉換爲Transbase
- 22. 我需要將NSUInteger轉換爲size_t嗎?
- 23. 從非.NET客戶端訪問WCF服務(ex.JAVA,HTML5)需要什麼步驟?
- 24. 爲什麼要將null轉換爲Object?
- 25. Gradle:Maven轉換爲Gradle之後,接下來的步驟是什麼?
- 26. 什麼是從使用libglade轉換爲GtkBuilder的步驟? (蟒蛇)
- 27. 爲什麼不需要將對象轉換爲JSON @XmlRootElement
- 28. 爲什麼我需要HTML5音頻庫?
- 29. 應該採取哪些步驟將我的XML轉換爲核心數據對象?
- 30. 爲什麼需要在ArrayList的構造中將類型轉換爲Object數組?
我已經創建了服務合同。實施它。我在主持人處遇到問題。當你創建一個WCF應用程序時,它會爲你做。我想修改一個類庫應用程序來做到這一點。 – 2009-11-18 22:10:50
您仍在爲服務啓動一個exe文件並實例化該服務。這就是我的問題,我需要Visual Studio自己弄清楚它在WCF服務庫中的作用。 – 2009-11-19 14:58:28