我已经将我的项目复制到一台干净的Windows 10机器上,只安装了Visual Studio 2015社区和SQL Server 2016 Express.除了与Windows 10和VS2015或SQL Server一起安装的版本之外,没有安装任何其他框架版本.
当我尝试启动WebApi项目时,我收到消息:
无法加载文件或程序集"System.Net.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a"或其依赖项之一.该系统找不到指定的文件.
该项目的包包括:
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)
使用.NET Framework 4.6.1构建项目后,System.Net.Http在该bin文件夹中找不到该文件.
该文件的路径指向:
C:\ Program Files(x86)\ Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll
该文件的路径System.Net.Http.Formatting指向:
C:\开发\ MyApp的\包\ Microsoft.AspNet.WebApi.Client.5.2.3\LIB \net45\System.Net.Http.Formatting.dll
整个项目是否应该以4.5.1为目标,还是有另一种方式来引用正确的组件?
我有一个Web API项目,它引用了我的模型和DAL程序集.向用户呈现登录屏幕,他可以在其中选择不同的数据库.
我按如下方式构建连接字符串:
public void Connect(Database database)
{
//Build an SQL connection string
SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
{
DataSource = database.Server,
InitialCatalog = database.Catalog,
UserID = database.Username,
Password = database.Password,
};
//Build an entity framework connection string
EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
{
Provider = database.Provider,
Metadata = Settings.Default.Metadata,
ProviderConnectionString = sqlString.ToString()
};
}
Run Code Online (Sandbox Code Playgroud)
首先,我如何实际更改数据上下文的连接?
其次,由于这是一个Web API项目,连接字符串(在每个登录时设置)在整个用户的交互中是持久的还是应该每次都传递到我的数据上下文?
在将OData添加到我的项目之前,我的路由设置如下:
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" },
handler: sessionHandler
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "Get" },
constraints: null,
handler: sessionHandler
);
config.Routes.MapHttpRoute(
name: "ApiByIdAction",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" },
handler: sessionHandler
Run Code Online (Sandbox Code Playgroud)
所有控制器都提供Get,Put(操作名称为Create),Patch(操作名称为Update)和Delete.例如,客户端使用这些各种标准URL来表示CustomerType请求:
string getUrl = "api/CustomerType/{0}";
string findUrl = "api/CustomerType/Find?param={0}";
string createUrl = "api/CustomerType/Create";
string updateUrl = "api/CustomerType/Update";
string deleteUrl …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class DatabaseFactory<C> : Disposable, IDatabaseFactory<C> where C : DbContext, BaseContext, new()
{
private C dataContext;
private string connectionString;
public DatabaseFactory(string connectionString)
{
this.connectionString = connectionString;
}
public C Get()
{
return dataContext ?? (dataContext = Activator.CreateInstance(typeof(C), new object[] {connectionString}) as C);
}
protected override void DisposeCore()
{
if (dataContext != null)
dataContext.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试启动web api时,出现以下错误:
Can't create component 'MyApp.DAL.Implementations.DatabaseFactory'1' as it has dependencies to be satisfied. 'MyApp.DAL.Implementations.DatabaseFactory'1' is waiting for the following dependencies:
- Parameter 'connectionString' which …
我有3个片段(A,B,C)的活动.片段A由a ViewPager和2组成ListFragments.用户可以点击任何listfragments中的项目,然后执行此操作,转到片段B.
在片段AI做:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
pagerAdapter = new PagerAdapter(getActivity().getSupportFragmentManager());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentA, container, false);
vpPager = (ViewPager)view.findViewById(R.id.vpPager);
vpPager.setOffscreenPageLimit(2);
vpPager.setAdapter(pagerAdapter);
vpPager.addOnPageChangeListener(this);
return view;
}
Run Code Online (Sandbox Code Playgroud)
以下PagerAdapter是:
private class PagerAdapter extends FragmentPagerAdapter {
private final ListFragment1 lf1 = ListFragment1 .newInstance();
private final ListFragment2 lf2 = ListFragment2 .newInstance();
public PagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) …Run Code Online (Sandbox Code Playgroud) 在Castle中,我曾经做过以下操作来注册来自不同程序集的类型:
Classes.FromAssemblyNamed("MyServer.DAL")
.Where(type => type.Name.EndsWith("Repository"))
.WithServiceAllInterfaces()
.LifestylePerWebRequest(),
Run Code Online (Sandbox Code Playgroud)
在Autofac中,我将上面的代码更改为:
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.Name.EndsWith("Repository"))
.InstancePerRequest();
Run Code Online (Sandbox Code Playgroud)
这是对的吗?
我有一个 NET6 项目,它是更大的 .NET 6 ASP.NET 解决方案的一部分。该项目仍然引用:
现在已被标记为已弃用。
我需要安装哪些软件包来替换它们?
问题在于,当前 SignalR 位于与主 ASP.NET 项目分开的程序集中。这是因为解决方案中的主项目和其他几个项目使用集线器(使用构造函数 DI)。
如果我将 SignalR 项目更改为
<Project Sdk="Microsoft.NET.Sdk.Web">
...
</Project>
Run Code Online (Sandbox Code Playgroud)
我收到以下编译错误:
错误 CS5001 程序不包含适合入口点的静态“Main”方法
所以问题是我无法与多个其他项目引用的 SignalR 建立一个通用程序集。
我正在尝试启用迁移,但它正在抛出异常:
检查上下文是否以现有数据库为目标... System.TypeInitializationException:'System.Data.Entity.Migrations.DbMigrationsConfiguration`1'的类型初始值设定项引发异常.---> System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常.---> System.Configuration.ConfigurationErrorsException:配置系统初始化失败---> System.Configuration.ConfigurationErrorsException:必须在'section'标签上指定'name'属性.
我假设App.config文件没有正确设置(当我添加EF包时它自动设置).我所做的就是添加连接字符串:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section Name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add Name="MyContext" connectionString="data source=MYSERVER;initial catalog=CodeFirstTest;user id=***;password=***;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我正在使用SQL Server 2008 R2.
因为我有一个连接字符串,我不相信我需要defaultconnectionfactory.我对么?(注意:即使没有本节,我仍然会得到相同的例外)
我还缺少什么?
我有一个循环,在循环内我正在做:
await Task.Delay(1000, ct).ContinueWith(async _ =>
{
await SecondMethodAsync(ct);
});
Run Code Online (Sandbox Code Playgroud)
第二种方法使用 EF 获取实体,设置一些属性并通过await SaveChangesAsync()调用DbContext.
上面应该等待1s然后继续第二种方法。通过上述实现,我得到以下异常:
在上一个异步操作完成之前,第二个操作在此上下文上启动。使用“await”确保在此上下文上调用另一个方法之前所有异步操作已完成。不保证任何实例成员都是线程安全的。
当我将上面的内容更改为:
await Task.Delay(1000, ct);
await SecondMethodAsync(ct);
Run Code Online (Sandbox Code Playgroud)
一切正常。
上面的两个片段有什么区别?如何使第一个片段起作用?
我有一个主要片段,里面有一个viewpager.此viewpager有2页(列表片段).当我启动激活时,会显示主片段,并且还会显示第一个分页片段.此分页片段使用db显示数据AsyncTask.
在主要片段中我有:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
onPageSelected(0);
}
@Override
public void onPageSelected(int position) {
Fragment fragment = (Fragment) pagerAdapter.instantiateItem(viewPager, position);
if (fragment instanceof IPagedFragment) {
((IPagedFragment) fragment).onShown(getActivity());
}
}
Run Code Online (Sandbox Code Playgroud)
界面是:
public interface IPagedFragment {
void onShown(FragmentActivity activity);
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是我必须将活动作为参数传递,因为在onShown调用时,活动仍为空.
此外,分页片段使用类似于LoginActivity样本的进度条逻辑.我也得到以下异常:
IllegalStateException:片段PagedFragment1 {4201f758}未附加到android.support.v4.app.Fragment.getResources上的Activity(Fragment.java:620)
那么,一旦分页片段完全可用于UI,开始从db检索数据的正确阶段是什么?
c# ×7
android ×2
.net-6.0 ×1
asp.net ×1
asp.net-core ×1
async-await ×1
asynchronous ×1
autofac ×1
odata ×1
signalr ×1