MySQL、Fluently NHibernate、WebAPI、Autofac,对我来说每一个都是麻烦疙瘩,现在它们为了一个共同的项目而凑合到一起了。一路磕磕碰碰,现在貌似有了一点眉目。
作为一个步入老人痴呆帕金森阶段的老革命,我当然要马上将奋斗过程记录下来:
1、MySql + Fluently NHibernate
static ISessionFactory sessionFactory;public static ISession OpenSession(string connString, string[] assemblys) { if (sessionFactory == null) { sessionFactory = Fluently.Configure() .Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard. ConnectionString(connString)) .Mappings(m => { foreach (var item in assemblys) { m.FluentMappings.AddFromAssembly(Assembly.Load(item)); } }).BuildSessionFactory(); } return sessionFactory.OpenSession(); } OpenSession((connString: "server=192.168.0.211; user id=root; password=lt1234; database=pnavrds", assemblys: new string[] { "Pnavrds.Data" });
.NET和NHibernate并不天然支持mysql,所以要在项目添加对mysql.data.dll的引用。mysql.data.dll在mysql的安装目录里有。
比如在 C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5
2、WebAPI
有关路由问题。
别看api与MVC很像,但是,MVC支持Area,而api并不。
但是开始时我并不知道。轻车熟路地加了个Area,一访问,直接404。
路由如下:
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Test_default", "Test/{controller}/{id}", new { id = UrlParameter.Optional } ); }
咋办呢?难道各种控制器济济一堂一锅粥?后来网上查了资料,添加了一个路由,改为:
public override void RegisterArea(AreaRegistrationContext context) { context.Routes.MapHttpRoute( "Test_defaultAPI", "api/Test/{controller}/{id}", new { id = RouteParameter.Optional } ); context.MapRoute( "Test_default", "Test/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }
注意,这样处理之后,同一个控制器,就有两个地址都可以访问。一个有区域,一个没有区域:
http://localhost/Pnavrds.API/api/Test/Dev3/10http://localhost/Pnavrds.API/api/Dev3/10
因为asp.net webapi并不支持区域,不管你这个控制器放在哪个文件夹、哪个命名空间下,它都顽强地解释到根目录下。我们上面做的努力,仅仅是多了一个含有区域名称的地址而已。
参考资料
3、Autofac
这个东东是个好东东。我现在都有点离不开它了。不然那么多实例需要构造,然后每个构造函数都N多参数,太麻烦。但是,因为了解不够,每次用它,好像都要费一些周折,并且很难调试。
这次也不例外。
1)提示System.Web.Http的版本不对。
引用的system.web.http.dll版本为5.2.3.0,但系统说跟5.2.0.0对应不上,编译时虽然可以通过,但有警告,建议在app.config里写些啥啥啥。我找遍了代码,都看不到哪里声明了5.2.0.0。
后来还是根据编译器的提示,将它给出的代码,加到web.config里,编译警告就没有了,运行就再无这个错误:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Iesi.Collections" culture="neutral" publicKeyToken="aa95f207798dfdb4" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Autofac" culture="neutral" publicKeyToken="17863af14b0044da" /> <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> </runtime>
附上编译信息:
4>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1819,5): warning MSB3247: 发现同一依赖程序集的不同版本间存在冲突。 在 Visual Studio 中,请双击此警告(或选择此警告并按 Enter)以修复冲突;否则,请将以下绑定重定向添加到应用程序配置文件中的“runtime”节点: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Iesi.Collections" culture="neutral" publicKeyToken="aa95f207798dfdb4" /><bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /></dependentAssembly></assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Autofac" culture="neutral" publicKeyToken="17863af14b0044da"/> <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" /></dependentAssembly></assemblyBinding>
2)说控制器没有默认构造函数
这说明autofac没有正常运行,否则不会报这个错。构造实例正是autofac的工作。
后来改了autofac的builder内容。代码如下:
public class AutofacConfig { public static void BuildContainer() { var builder = new ContainerBuilder(); //Infrastructure objects builder.RegisterApiControllers(typeof(WebApiApplication).Assembly); builder.RegisterAssemblyTypes(typeof(WebApiApplication).Assembly).AsImplementedInterfaces(); builder.RegisterModule(new AutofacWebTypesModule()); //其他代码..... builder.RegisterModelBinderProvider(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = (new AutofacWebApiDependencyResolver(container)); } }
MySQL、Fluently NHibernate、WebAPI、Autofac,对我来说每一个都是麻烦疙瘩,现在它们为了一个共同的项目而凑合到一起了。一路磕磕碰碰,现在貌似有了一点眉目。
作为一个步入老人痴呆帕金森阶段的老革命,我当然要马上将奋斗过程记录下来:
1、MySql + Fluently NHibernate
static ISessionFactory sessionFactory;public static ISession OpenSession(string connString, string[] assemblys) { if (sessionFactory == null) { sessionFactory = Fluently.Configure() .Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard. ConnectionString(connString)) .Mappings(m => { foreach (var item in assemblys) { m.FluentMappings.AddFromAssembly(Assembly.Load(item)); } }).BuildSessionFactory(); } return sessionFactory.OpenSession(); } OpenSession((connString: "server=192.168.0.211; user id=root; password=lt1234; database=pnavrds", assemblys: new string[] { "Pnavrds.Data" });
.NET和NHibernate并不天然支持mysql,所以要在项目添加对mysql.data.dll的引用。mysql.data.dll在mysql的安装目录里有。
比如在 C:\Program Files (x86)\MySQL\Connector.NET 6.9\Assemblies\v4.5
2、WebAPI
有关路由问题。
别看api与MVC很像,但是,MVC支持Area,而api并不。
但是开始时我并不知道。轻车熟路地加了个Area,一访问,直接404。
路由如下:
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Test_default", "Test/{controller}/{id}", new { id = UrlParameter.Optional } ); }
咋办呢?难道各种控制器济济一堂一锅粥?后来网上查了资料,添加了一个路由,改为:
public override void RegisterArea(AreaRegistrationContext context) { context.Routes.MapHttpRoute( "Test_defaultAPI", "api/Test/{controller}/{id}", new { id = RouteParameter.Optional } ); context.MapRoute( "Test_default", "Test/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }
注意,这样处理之后,同一个控制器,就有两个地址都可以访问。一个有区域,一个没有区域:
http://localhost/Pnavrds.API/api/Test/Dev3/10http://localhost/Pnavrds.API/api/Dev3/10
因为asp.net webapi并不支持区域,不管你这个控制器放在哪个文件夹、哪个命名空间下,它都顽强地解释到根目录下。我们上面做的努力,仅仅是多了一个含有区域名称的地址而已。
参考资料
3、Autofac
这个东东是个好东东。我现在都有点离不开它了。不然那么多实例需要构造,然后每个构造函数都N多参数,太麻烦。但是,因为了解不够,每次用它,好像都要费一些周折,并且很难调试。
这次也不例外。
1)提示System.Web.Http的版本不对。
引用的system.web.http.dll版本为5.2.3.0,但系统说跟5.2.0.0对应不上,编译时虽然可以通过,但有警告,建议在app.config里写些啥啥啥。我找遍了代码,都看不到哪里声明了5.2.0.0。
后来还是根据编译器的提示,将它给出的代码,加到web.config里,编译警告就没有了,运行就再无这个错误:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Iesi.Collections" culture="neutral" publicKeyToken="aa95f207798dfdb4" /> <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Autofac" culture="neutral" publicKeyToken="17863af14b0044da" /> <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" /> </dependentAssembly> </assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> </runtime>
附上编译信息:
4>C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(1819,5): warning MSB3247: 发现同一依赖程序集的不同版本间存在冲突。 在 Visual Studio 中,请双击此警告(或选择此警告并按 Enter)以修复冲突;否则,请将以下绑定重定向添加到应用程序配置文件中的“runtime”节点: <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly></assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Iesi.Collections" culture="neutral" publicKeyToken="aa95f207798dfdb4" /><bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" /></dependentAssembly></assemblyBinding> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Autofac" culture="neutral" publicKeyToken="17863af14b0044da"/> <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" /></dependentAssembly></assemblyBinding>
2)说控制器没有默认构造函数
这说明autofac没有正常运行,否则不会报这个错。构造实例正是autofac的工作。
后来改了autofac的builder内容。代码如下:
public class AutofacConfig { public static void BuildContainer() { var builder = new ContainerBuilder(); //Infrastructure objects builder.RegisterApiControllers(typeof(WebApiApplication).Assembly); builder.RegisterAssemblyTypes(typeof(WebApiApplication).Assembly).AsImplementedInterfaces(); builder.RegisterModule(new AutofacWebTypesModule()); //其他代码..... builder.RegisterModelBinderProvider(); builder.RegisterFilterProvider(); IContainer container = builder.Build(); //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = (new AutofacWebApiDependencyResolver(container)); } }
以上就是mysql + Fluently NHibernate + WebAPI + Autofac的内容,更多相关内容请关注PHP中文网(www.php.cn)!

ストアドプロシージャは、パフォーマンスを向上させ、複雑な操作を簡素化するためのMySQLのSQLステートメントを事前に拡大します。 1。パフォーマンスの改善:最初のコンピレーションの後、後続の呼び出しを再コンパイルする必要はありません。 2。セキュリティの改善:許可制御を通じてデータテーブルアクセスを制限します。 3.複雑な操作の簡素化:複数のSQLステートメントを組み合わせて、アプリケーションレイヤーロジックを簡素化します。

MySQLクエリキャッシュの実用的な原則は、選択クエリの結果を保存することであり、同じクエリが再度実行されると、キャッシュされた結果が直接返されます。 1)クエリキャッシュはデータベースの読み取りパフォーマンスを改善し、ハッシュ値を使用してキャッシュされた結果を見つけます。 2)単純な構成、mysql構成ファイルでquery_cache_typeとquery_cache_sizeを設定します。 3)SQL_NO_CACHEキーワードを使用して、特定のクエリのキャッシュを無効にします。 4)高周波更新環境では、クエリキャッシュがパフォーマンスボトルネックを引き起こし、パラメーターの監視と調整を通じて使用するために最適化する必要がある場合があります。

MySQLがさまざまなプロジェクトで広く使用されている理由には、次のものがあります。1。複数のストレージエンジンをサポートする高性能とスケーラビリティ。 2。使いやすく、メンテナンス、シンプルな構成とリッチツール。 3。豊富なエコシステム、多数のコミュニティとサードパーティのツールサポートを魅了します。 4。複数のオペレーティングシステムに適したクロスプラットフォームサポート。

MySQLデータベースをアップグレードする手順には次のものがあります。1。データベースをバックアップします。2。現在のMySQLサービスを停止します。3。MySQLの新しいバージョンをインストールします。アップグレードプロセス中に互換性の問題が必要であり、Perconatoolkitなどの高度なツールをテストと最適化に使用できます。

MySQLバックアップポリシーには、論理バックアップ、物理バックアップ、増分バックアップ、レプリケーションベースのバックアップ、クラウドバックアップが含まれます。 1. Logical BackupはMySqldumpを使用してデータベースの構造とデータをエクスポートします。これは、小さなデータベースとバージョンの移行に適しています。 2.物理バックアップは、データファイルをコピーすることで高速かつ包括的ですが、データベースの一貫性が必要です。 3.インクリメンタルバックアップは、バイナリロギングを使用して変更を記録します。これは、大規模なデータベースに適しています。 4.レプリケーションベースのバックアップは、サーバーからバックアップすることにより、生産システムへの影響を減らします。 5. Amazonrdsなどのクラウドバックアップは自動化ソリューションを提供しますが、コストと制御を考慮する必要があります。ポリシーを選択するときは、データベースサイズ、ダウンタイム許容度、回復時間、および回復ポイントの目標を考慮する必要があります。

mysqlclusteringenhancesdatabaserobustnessnessnessnessnessnistandistributiondistributingdataacrossmultiplenodes.itesthendbenginefordatareplication andfaulttolerance、保証highavailability.setupinvolvesconfiguringmanagement、data、ssqlnodes、carefulmonitoringringandpe

MySQLのデータベーススキーマ設計の最適化は、次の手順を通じてパフォーマンスを改善できます。1。インデックス最適化:一般的なクエリ列にインデックスを作成し、クエリのオーバーヘッドのバランスをとり、更新を挿入します。 2。テーブル構造の最適化:正規化または反通常化によりデータ冗長性を削減し、アクセス効率を改善します。 3。データ型の選択:Varcharの代わりにINTなどの適切なデータ型を使用して、ストレージスペースを削減します。 4。パーティション化とサブテーブル:大量のデータボリュームの場合、パーティション化とサブテーブルを使用してデータを分散させてクエリとメンテナンスの効率を改善します。

tooptimizemysqlperformance、soflowthesesteps:1)properindexingtospeedupqueries、2)useexplaintoanalyzeandoptimize Queryperformance、3)AductServerContingSettingStingsinginginnodb_buffer_pool_sizeandmax_connections、4)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ホットトピック









